anonymous1337 wrote:caseih wrote:FB doesn't offer generics in OOP (and do you really want templates on FB?).
Generics =/= C++ Templates
(Generics is more than just C#. E.g. Ada. However in general it
does seems to be a bit more declarative then C++ templates. I usually use generics for the general term, and templates for the C++ implementation specifically)
You can pick and choose, based on the needs of the language and its users, what features or syntax you want for generics. C++ templates are apparently scary. I haven't made heavy use of them (and when I did, I thought it was cool), but I've seen a lot of veteran programmers complain about their over-use. (Then again, these capabilities help make C++ very powerful.)
I'm no real expert (let alone knowing implementation details), so the below is more an summary of impressions than cold hard facts:
The core problem is that if you have a something<bla>, where bla is an abstract generic type and in some generic method do bla+bla, where and how is the operator defined? Must the operator be defined and declared when the generic is defined, or must it only be defined in the context of the specialization?
Afaik the aversion against C++ templates is that templates must be based on token replay (since the template declaration is not declarative enough to do much typechecking), so more (usage/specialization moment) context dependent. This makes implementation costly, compilation slow etc, unintentional sideeffect likely etc. It also makes reusing already generated code hard if the template is differenly declared but generates the same code (e.g. a container<child> and container<parent>).
That is what most online resources rave about, but mostly from the viewpoint of python or C#/Java.
The trouble is that the C#/Java Generics model also has sides that are a bad fit for FB because:
- FB is a compiled language and compilation speed is less important than with JITs.
- Java and C# have a mostly single rooted type system based on a root class, and valuetypes are autoboxed to class equivalents. They have no pointer types, and struct types are limited (and very class like or autoboxed again as needed). Note that class types are automatically reference types in these languages.
The last bit makes it fairly easy to have generics with class as base entity (iow you can only specialize a generic with a class and other types are autoboxed if necessary). But the FPC, C++ and I assume FB too typesystems don't work that way. Value types and non class aggregate types play a vital role in the language.
Delphi went the (C#/Java) generic way, and the implementation feels toothless and the worst of both worlds. On the other side I get the feeling that many devels shy away from the massive complexity of the C++ combination of typesystem and templates.
I would be fine with Java-ish or C#-like generics. The simpler to start off, the better, IMO.
I think the first goal should be typesafe container types, though so forms of type coercion go a long way for that too.