Enum Operator

Forum for discussion about the documentation project.
Post Reply
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Enum Operator

Post by sancho3 »

The operators page describes the operators overloadable like so:

Code: Select all

Syntax:
{ Type | Class | Union | Enum } typename
declare Operator cast () [ byref ] as datatype
declare Operator @ () [ byref ] as datatype ptr
declare Operator assignment_op ( [ byref | byval ] rhs as datatype )
declare Operator [] ( index as datatype ) [ byref ] as datatype
declare Operator new ( size as uinteger ) as any ptr
declare Operator new[] ( size as uinteger ) as any ptr
declare Operator delete ( buf as any ptr )
declare Operator delete[] ( buf as any ptr )
End { Type | Class | Union | Enum }
Isn't this saying that code like so is possible:

Code: Select all

enum enum_type
	none
	one
	two
	three
	declare operator cast() as string  ' this will cause an error
end enum 
There is no way to overload the cast operator for an enum.
This issue has been mentioned before here, but I don't see a resolution.

Edit: Whoops, you are right Mr.Swiss. I wrote it wrong. Now I add the 'declare' so that even though its not allowed the question makes more sense.

It seems that both 'enum' and 'class' are included in this part of the documentation for the future when enum can accept member procedures and Class becomes a legitimate keyword.
Last edited by sancho3 on Jan 08, 2018 20:53, edited 2 times in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Enum Operator

Post by MrSwiss »

I'm not absolutely certain but, the way you wrote it:
The Enum can't possibly know: which Type(s) Operator cast(), you are referring to.

Try: TypeName.Operator ...
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Enum Operator

Post by fxm »

At present time, Enum does not accept any member procedure.
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Enum Operator

Post by fxm »

Workaround:

Code: Select all

type enum_class
  enum enum_type
    none
    one
    two
    three
  end enum
  dim as enum_type e
  declare operator cast() as string
end type

operator enum_class.cast() as string
  return str(this.e)
end operator

dim as enum_class ec
ec.e = enum_class.two

print ec

sleep
Post Reply