The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My work

  • My badges

  • Twitter Updates

  • My Flickr Stream

    20140417-Windows-8.1-Update-says-it-failed-but-it-succeeded

    20140417-Windows-8.1-Update-says-it-failed-but-it-succeeded--Windows-Help-and-Support--cannot-find-any-info--how-surprising-it-never-does

    20140329-VMware-vSphere-Client-4.1-retry-with-compatibility-settings

    More Photos
  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,250 other followers

Delphi operator overloading: table of operators, names, and some notes on usage and ‘glitches’

Posted by Jeroen Pluimers on 2009/10/19

Operator overloading is a very nice feature of the Delphi language.
However. the Delphi documentation on Operator overloading is not completely right.

Below is my table of what I found out so far, and some notes.

It is part of my “Nullable types in Delphi” session that I gave on some conferences.
The downloads for that session contain more detailed information.

This is just an abstract to get you going and a try to answer this operator overloading question on Stackoverflow.
Download the full presentation to get more in depth information.

Let me know if you need more information.

Notes

Operator overloading

Add your own “behaviour” to operators

  • Win32: Works only for records, not classes!
  • An operator and the operand(s)
    are being implemented worden by a “class operator”;
    this is a kind of class method with name and argumen(s)

Example:

  • Multiplication X : = A * B;
  • Operator: *
  • Name: Multiply
  • Operands: 2 -> two parameters
type
  TMyRecord = record
    class operator Multiply(A, B: TMyRecord): TMyRecord;
  end;

Documentation is not correct!

Combining the rules of operator and result types, you can do magical things like Dances with XML | Australian Delphi User Group Members.

Do not use Delphi 2006 with operator overloading

Delphi 2007 fixed a number of bugs including this one: Delphi 2006 wont allow const parameters of type record within record method? – Stack Overflow.

Watch the result type of comparison operators!

Tips:

  • Some operators should be overloaded pair-wise
    = and <>
    shl and shr
    < and >=
    > and <=
    dec and inc
    + and -
    / and *
    div and mod
  • Prefer Explicit over Implicit operators
    • Beware of the built-in type coercion (implicit operators)
    • e.g
      • Byte to Integer;
      • Integer to Double;
      • Variants from/to anything!

Table of operators

operator # usage name cagetory *
and 2 R := A and B; BitwiseAnd bit
not 1 R := not A; //BitwiseNot bit glitch: does not exist!
or 2 R := A or B; BitwiseOr bit
xor 2 R := A xor B; BitwiseXor bit
() cast 1 R := TValue(A); Explicit conversion
:= 1 R := A; Implicit conversion
operator # usage name category *
round 1 R := Round(A); Round function
trunc 1 R := Trunc(A); Trunc function
and 2 R := A and B; LogicalAnd logical
not 1 R := not A; LogicalNot logical
or 2 R := A or B; LogicalOr logical
xor 2 R := A xor B; LogicalXor logical
operator # usage name category *
+ 2 R := A + B; Add binary
/ 2 R := A / B; Divide binary
div 2 R := A div B; IntDivide binary
mod 2 R := A mod B; Modulus binary
* 2 R := A * B; Multiply binary
- 2 R := A – B; Subtract binary
operator # usage name category *
shl 2 R := A shl B; LeftShift binary name is confusing
shr 2 R := A shr B; RightShift binary name is confusing
- 1 R := -A; Negative binary
+ 1 R := +A; Positive binary
dec 1 Dec(A); Dec self
inc 1 Inc(A); Inc self
operator # usage name category *
= 2 R := A = B; Equal comparison
> 2 R := A > B; GreaterThan comparison
>= 2 R := A >= B; GreaterThanOrEqual comparison
< 2 R := A < B; LessThan comparison
<= 2 R := A <= B; LessThanOrEqual comparison
<> 2 R := A <> B; NotEqual comparison

–jeroen

4 Responses to “Delphi operator overloading: table of operators, names, and some notes on usage and ‘glitches’”

  1. This article is now outdated. It should updated according to the current Delphi version.
    See my blog http://francois-piette.blogspot.be/2013/02/operator-overloading-for-records.html

    • jpluimers said

      I’ve updated the documentation links to the most current Delphi versions. Please let me know if there were more outdated parts in the information.

      • The sentence “At least one operand…” is incorrect. You can have for example
        class operator Implicit(AValue : BYTE) : TData;
        The only operand is of type byte.

      • jpluimers said

        For single operand operators, my statement is still correct (;
        I’ll rephrase to make it more clear though.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 1,250 other followers

%d bloggers like this: