Jump to content
apokroy

Operator overloading for enumerations

Recommended Posts

Hello!

Is there any way to overload operators for enumerations?

I'm having fun trying to implement a Tri-State Boolean and Optional types, but I'm forced to use a record type for operators. Records look a bit ugly, have performance, RTTI and published properties issues ...

Now, my code looks like this:

TriBoolEnum = (Unknown, ExactFalse, ExactTrue);
TriBoolSet  = set of TriBoolEnum;

TriBool = record
private
  FValue: TriBoolEnum;
public
  class operator Initialize(out Value: TriBool);
  class operator Implicit(const Value: Boolean): TriBool; static; inline;
  class operator Implicit(const Value: TriBool): Boolean; static; inline;
  class operator Implicit(const Value: TriBool): TriBoolEnum; static; inline;
  class operator Implicit(const Value: TriBoolEnum): TriBool; static; inline;
  class operator Equal(const L, R: TriBool): TriBool; static; inline;
  class operator Equal(const L: Boolean; R: TriBool): TriBool; static; inline;
  class operator Equal(const L: TriBool; R: Boolean): TriBool; static; inline;
  class operator LogicalOr(const L, R: TriBool): TriBool; static; inline;
  class operator LogicalOr(const L: Boolean; R: TriBool): TriBool; static; inline;
  class operator LogicalOr(const L: TriBool; R: Boolean): TriBool; static; inline;
...

I want something like this:

TriBool = (Unknown, ExactFalse, ExactTrue);

TriBoolHelper = record helper for TriBool
class operator ...

It compiles, but overloading doesn't work.

 

Thanks in advance.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×