[fpc-devel] Generic class comparison operators

Ryan Joseph genericptr at gmail.com
Sat Apr 17 22:39:51 CEST 2021



> On Apr 17, 2021, at 2:12 PM, Jonas Maebe via fpc-devel <fpc-devel at lists.freepascal.org> wrote:
> 
> The issue with allowing it for classes (generic or not) is that the the = operator already has a meaning for them (pointer equality). I think in general we don't allow overloading operators that have a built-in meaning.

I see your point about there being a conflict for pointer quality but you can actually resolve it by casting to TObject, however as you note it could be confusing also. That's an interesting question I'd need to think about more but even so that is only the = operator with other pointer types.

Here's the list of possible operators that don't have side effects to the caller and are worth considering and may be safe.

class operator = (left: TSelf; right: TRight): boolean;
class operator not (left: TSelf): boolean;
class operator <> (left: TSelf; right: TRight): boolean;
class operator < (left: TSelf; right: TRight): boolean;
class operator > (left: TSelf; right: TRight): boolean;
class operator <= (left: TSelf; right: TRight): boolean;
class operator >= (left: TSelf; right: TRight): boolean;
class operator >< (left: TSelf; right: TRight): boolean;
class operator in (left: TSelf; right: TRight): boolean;


Regards,
	Ryan Joseph



More information about the fpc-devel mailing list