16

I wonder what sort of operator overloads are possible (and with what version of Delphi)? Thanks to Hallvard's great write-up on operator overloading, I know of :

  • Add(Left, Right: Type): Type (for the '+' operator)
  • Subtract(Left, Right: Type): Type (for the '-' operator)
  • Multiply(Left, Right: Type): Type (for the '*' operator)
  • Divide(Left, Right: Type): Type (for the '/' operator)
  • Implicit(aValue: Type): AnotherType and
  • Implicit(aValue: AnotherType): Type (for implicit assignments : a := b)
  • Explicit(aValue: Type): AnotherType and
  • Explicit(aValue: AnotherType): Type (for explicit assignmetns of 'casts' : a := Type(b)

However, what I don't know are the names for the '=', '<=', '<', '<>', '>' and '>=' operators. Do these exist, and from what Delphi version can I use these?

PS: I still use Delphi 2009 at the moment, so I would have another strong upgrade-argument if 2010 offers these ;-)

2 Answers 2

17

Actually, I found the answer to this myself, but kept the question as I can imagine people will search for this information regularly on stackoverflow;

The official description on operator overloaders can be found here : http://docwiki.embarcadero.com/RADStudio/en/Operator_Overloading_(Delphi)

Mainly, the ones I was looking for are named:

  • Equal, for '=' comparison : Equal(a: type; b: type) : Boolean;
  • NotEqual, for '<>' comparison : NotEqual(a: type; b: type): Boolean;
  • GreaterThan, for '>' comparison : GreaterThan(a: type; b: type) Boolean;
  • GreaterThanOrEqual, for '>=' comparison : GreaterThanOrEqual(a: type; b: type): resultType;
  • LessThan, for '<' comparison : LessThan(a: type; b: type): resultType;
  • LessThanOrEqual, for '<=' comparison : LessThanOrEqual(a: type; b: type): resultType;
1
  • Page has moved here
    – pritaeas
    Commented Jan 18, 2012 at 13:07
10

I just put my notes online of a session on nullable types with operator overloading that I gave during a few conferences.

It now contains a table of the operators, the names and what works/doesn't work. WordPress initially malformed that table, but I managed to restore it.

You can also download the full set of slides and source code demos.

In short:

  • The documentation (even the DocWiki) is not complete, and note reliable
  • You cannot overload the bitwise not operator, as BitwiseNot is not supported by the compiler. You have to overload the logical not operator in stead.
  • Overload operators pairwise where applicable
  • Be aware of existing operators
  • Be very careful when implementing implicit operators

Hope this helps you. Let me know if you need more info: I have done quite a bit of production work with operator overloading.

--jeroen

3
  • Thanks - now that you mention it, I've seen your slides before. All in all a good overview. It's a pity that the Delphi-community has to document this; I would have expected an even more thorough piece of documentation from CodeGear/Embarcadero instead.
    – PatrickvL
    Commented Oct 19, 2009 at 13:56
  • I need to find time to get loads of my stuff on-line first, then update the right Wiki's. Commented Oct 19, 2009 at 14:35
  • A bitwise not is AnInteger XOR -1
    – Johan
    Commented Dec 9, 2011 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.