What do people think about of following hack?

What do people think about of following hack?

Just read the following on BeginEnd: https://translate.google.com/translate?sl=ja&tl=en&u=http%3A%2F%2Fd%2Ehatena%2Ene%2Ejp%2Ftales%2F20160812%2F1470932247

I was just struck by the creativity of abusing the explicit typecast operator `()` as a makeshift function parameter brackets `()`.

class operator StrToEnum .Explicit ( const Value: string): StrToEnum ;
Combined witha implicit operator overload resolve the StrToEnum  

 
This allows us to fake a naked generic function.

X:= StrToEnum(string1);

Horrible hack?  or great code?

Comments

  1. All this just to get rid of typing TypeInfo(TMyEnum)... not my cup of tea... And the big caveat of this code is that if the string does not match an enumerate, it will fill the variable with $ff bytes... Pretty useless in practice for production code. Also note that the GetEnumNameValue() currently implemented in System.TypeInfo.pas is much slower than its previous versions, due to some unexpected memory allocations, and unneeded UTF-16<->UTF-8 conversions.

    ReplyDelete
  2. /sub interesting! tricky english tho

    ReplyDelete

Post a Comment