This is a rather bizarre notation:
var
lst : fgl.specialize TFPGList<Integer>;
begin
lst := fgl.specialize TFPGList<Integer>.Create;
end.
Is there an alternative notation that would be more "pascalish"? For example like this:
var
lst : specialize fgl.TFPGList<Integer>;
begin
lst := specialize fgl.TFPGList<Integer>.Create;
end.
The first way, where the keyword specialize is "mixed in" into part of the name, is not very readable.
You are not thinking far ahead enough: the problem is if you have multiple specialization in one statement. E.g.
SomeUnit.specialize SomeTypeA<TypeX>.specialize SomeTypeB<TypeY>.specialize SomeClassFunc<TypeZ>. By having the
specialize be part of the identifier to be specialized things are much clearer.
Delphi mode does not use this keyword. So was it really necessary to add this keyword? Perhaps the rule of "don't multiply entities beyond necessity" should have worked here (i.e. if the syntax of the language is readable without additional syntax elements, then you probably shouldn't add them).
First of the keyword was added
way before Delphi added generics. And second FPC still does not support all situations where Delphi supports specializations (e.g. expressions with inline specializations that involve operators), simply because the syntax of using “<” is
ambiguous especially due to Delphi also allowing overloads of generics types with variables, constants and functions.
However, if there are any reasons to use this word, then perhaps it should appear before the type name? After all, the fgl prefix only specifies the name of the module from which this class comes (it becomes part of the type name).
No, that was originally the case, but was changed with 3.2.0 when generic functions were introduced for the above reason.
or just drop fgl. .. not the library, but the prefixvar
lst : specialize TFPGList<Integer>;
even better, declare a type first. Keeps your code cleaner.{$mode objfpc}
uses fgl;
type
Tlst = specialize TFPGList<Integer>;
var
lst : Tlst;
begin
lst := Tlst.Create;
Lst.Free;
end.
The unit prefix only makes sense if you combine multiple generics libraries, like fgl, lgenerics and or rtl-generics and all of that also in the same unit....
Also this simply won't work in FPC < 3.3.1 due to it not handling unit names together with generics correctly (it will pick the generic from one of the two units, not necessarily the one you specified, assuming it's indeed available in multiple units).