Khorkhe 1 Posted April 18 Hello, When overloading operators in a Delphi record, it is possible to specify a result type that the documentation does not recognize: TCondRec = record public 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?) Thanks Share this post Link to post
Uwe Raabe 2001 Posted April 18 Although it is not written explicitly in the docs Quote 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
Remy Lebeau 1339 Posted April 18 (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 April 19 by Remy Lebeau Share this post Link to post
David Heffernan 2332 Posted April 19 Can you link the documentation you refer to so that there is no ambiguity in the question Share this post Link to post
Khorkhe 1 Posted April 19 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. https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Operator_Overloading_(Delphi) Share this post Link to post
Anders Melander 1733 Posted April 19 FWIW, I agree with @Khorkhe; The documentation for the Equal operator appears to be wrong. Share this post Link to post
pmcgee 10 Posted April 24 (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 April 24 by pmcgee Share this post Link to post