Jump to content

Record operator overloading, can use undocumented return type

Recommended Posts


When overloading operators in a Delphi record, it is possible to specify a result type that the documentation does not recognize:

  TCondRec = record
    Text: string;
    Params: TParams;
    class operator LogicalAnd (aLeft, aRight: TCondRec): TCondRec;  // return TCondRec instead of Boolean
    class operator LogicalOr (aLeft, aRight: TCondRec): TCondRec;   // here as well

This allows us to write expressions such as the below, to generate an SQL string as the result of joining `aLeft` and `aRight` with an `AND` or `OR` operator.

It seems this technique is internally used by EntityDAC components in order to provide an "SQL Builder", somewhat similar to the idea exposed here.


My worry however, is that this syntax showed above is not documented: according to the Embarcadero docs, these class operators are supposed to return a Boolean.


Here's some screenshots of what can be done:






The question is:

How concerned should I be about using such a library, in the eventuality that Embarcadero decides to enforce the syntax described by its documentation?

Or alternatively (if lucky), any chance the documentation itself is wrong? (docs are often known to be missing, maybe they just didn't write it correctly here?)



Share this post

Link to post

Although it is not written explicitly in the docs

  • For a logical operator and a bitwise operator using the same symbol, the logical operator is used only when the operands are booleans. Since the type of record for this record operator is not a boolean, a logical operator will only be used when the other operand is a boolean.

I assume the Logical operators are used as if they were Bitwise operators when the condition above is not met and no corresponding Bitwise operators are declared.


Nevertheless could you achieve the same using Bitwise operators in the first place.

Share this post

Link to post
Posted (edited)
On 4/18/2024 at 8:40 AM, Khorkhe said:

according to the Embarcadero docs, these class operators are supposed to return a Boolean.

Where do you see that documented?  According to this documentation, the return type of the LogicalAnd and LocicalOr operators is simply specified as "resultType", nothing says it is required to be Boolean, so it can be whatever type the implementation wants.

Edited by Remy Lebeau

Share this post

Link to post

Apologies, I made a mistake.

I was intending to refer to the Equal operator (and NotEqual), where the docs below say:

Equal(a: type; b: type) : Boolean; 

yet in the screenshots initially posted, EntityDAC apparently uses record operator overloading, to implement a compile-time checked SQL Builder that generates SQL strings.

However in this case, the Equal operator overload will return a string, that is:

Result := aLeftOperand + ' = ' + aRightOperand

This compiles and runs fine as per the screenshots above (and as per EntityDAC's components), but the documentation says it must return a boolean.




Share this post

Link to post
Posted (edited)

It is, in a sense, a bit sad that the lack of user-definable infix operators leads to this sort of "creative  interpretation".

Shouldn't we complain about this almost as much as about 'With' eg ?

This could have (should have?) been implemented by a JoinTo( s1, s2 : string) function.


An actual Equality Operator should exhibit the qualities of being reflexive (a=a), symmetric (a=b => b=a), and transitive (a=b, b=c => a=c)


(and going back to the first question ... it wouldn't need to return a boolean.  Eg some cases, like a partial order, it might return an optional boolean)

Edited by pmcgee

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