Missing in action: generic free functions.

Missing in action: generic free functions.

Why oh why can't I have such nice things in Delphi?

One thing I miss from C++ is dynamic_cast. This is easily implementable in Delphi using generics, but I can't make it a free function, and instead have to use some fugly syntax...

:(

Comments

  1. Currently, Generics is tied to classes - not sure why.

    ReplyDelete
  2. I know, and frankly it sucks ass. It leads to butt-ugly code, not helped by the poor type deduction. I mean TArray.Sort(myints)... barf

    That should read Sort(myints).

    ReplyDelete
  3. I guess it relates to the primitive types in Delphi not sharing a common "root" - but yeah - that could be hidden by the compiler.

    ReplyDelete
  4. Asbjørn Heid Just that is a case where I don't agree. I fear you have to prefix most of those Sort calls with the unit name to identify it.

    ReplyDelete
  5. Uwe Raabe Stop looking at the tree ;)

    Another example: In C++ you have dynamic_cast() function. I can't make a nice version of this in Delphi. Best I can do is 
      y := Dynamic.Cast(x);

    Which, while not as horrible as TArray.Sort<> isn't exactly lovely, as it abuses a keyword.

    ReplyDelete
  6. Lars Fosdal I don't buy it. Generic free functions are just overload sugar, sweet sweet sugar.

    ReplyDelete

Post a Comment