Maybe already posted here but i missed it

Maybe already posted here but i missed it

Anyway, I'm just shocked

Where the hell did that operators overloading come from? Why is not documented?

It worked on Delphi 2010 just fine, and on 10.1 Berlin Starter, so it's been around since a while

Did a quick search but couldn't find anything
https://gist.github.com/lynatan/886ed984d230ac1b42dd89ed42ab2214

Comments

  1. That's operator overloading for records, not for classes

    Even Marco stated that operator overloading is available only for records, see blog.marcocantu.com - Class Operators in Delphi

    The syntax used in the snippet is not documented. If you use the record syntax you get a compiler error

    ReplyDelete
  2. Oh, OK. I thought it only worked for classes on the nextgen/arc compilers?

    ReplyDelete
  3. Worked fine on Win32 compiler. There is no arc on Delphi 2010, yet it's working fine

    ReplyDelete
  4. Hmmm, I need to play with this further

    ReplyDelete
  5. Agustin Ortu​, Did you check if there is a memory leak?

    ReplyDelete
  6. That's fine because the operator doesn't return an instance of a class. It returns a boolean. I'd expect it to fail to compile for arithmetic operators that return instances.

    ReplyDelete
  7. Marco Cantù please comment this. How safe is it to use?

    ReplyDelete
  8. Peter Sokolov This is not an intentional feature and it is NOT safe to use imho. As explained, operator overloading is supported for records on all platform and for classes on ARC-based compilers. Now it is true that some operators are actually safe to use in terms of memory management... but I'd recommend not leveraging undocumented features (and later complain if we end up disabling them, on purpose or as side effect of other changes): use at your own risk!

    ReplyDelete
  9. This is a very nice hack because the compiler seems to just look at specifically named static methods on the type when it encounters operators. I have used this in a few places in Spring4D 1.2 like adding the equals operator to TValue or adding those operators to TGuid for the still supported Delphi2010 version where TGuid did not have them yet.

    And yes, you can abuse them pretty much like creating an Implicit operator for TStrings that accepts a string which then creates a TStringList, changing the equals operator for TObject that does not compare the reference anymore but something else or other crazy things >;o)

    ReplyDelete
  10. Maybe it is about time to have Delphi language specification... instead of "if it compiles it is valid Delphi code..." and then when it breaks...

    ReplyDelete
  11. Dalija Prasnikar Awww... but where would the fun be in that ;)?

    ReplyDelete
  12. Allen Bauer I am retracting my proposal. I am not a fun breaker ;)

    ReplyDelete
  13. Marco Cantù " This is not an intentional feature"

    Oh crap, the 32bit compiler has achieved sentience and is modifying its code! ;-)

    ReplyDelete
  14. Dalija Prasnikar Nowadays, even if it does compile, it's not necessarily considered valid Delphi code, as Marco demonstrated.

    ReplyDelete

Post a Comment