I always thought operator overloading only works for structural types. Has that changed?
Well I just can't overload + and - for some reason, but all the others I can:
{$mode objfpc}{$H+}
type
TMyEnum = (a,b,c);
operator * (const a,b:TMyEnum)c:TMyEnum;
begin
c := TMyEnum(ord(a) * ord(b));
end;
var
aa:TMyEnum;
begin
aa := b * c;
writeln(aa);
end.
This form of operator overloading is already an old feature from 2.x. and {$mode objfpc} not {$mode delphi}
What you refer to are probably class operators for records, objects and classes.
In the above case there seems to be an issue for addition and substraction only and I think that is a bug, because the compiler only won't accept addition and substraction.
- On the one hand it says impossible operator overload for these two, hinting at a default implementation
- On the other hand it says enums.pas Operation "-" not supported for types "TMyEnum" and "TMyEnum".
Fair enough, but all others can be overloaded: > >< = * div and or xor not in ** etc. Any thoughts this may warrant a bug report?
All this is despite Sven's explanation: seems a contradiction in terms, to some extend.
Anyway, ternary truth tables can be build from other operators.