What I had problems with was:
specialize to a type instead of a var and code verbosity between Delphi mode and ObjFPC mode.
What is not clear about that?
There are two distinct advantages for specializing to a type:
- After the specialization the code looks the same independent of mode Delphi or ObjFpc
- You get rid of the shoutiness and duplication of code that you have when specializing to a var.
program testspec1;
{$mode objfpc}
uses fgl;
type
TMyIntegerList = specialize TFPGlist<integer>;
var
L1:TMyIntegerList;
L2:specialize TFPGList<integer>;
begin
{ simple and clean syntax }
L1 := TMyIntegerList.Create;
{ dirty and verbose syntax because of duplication of specialize}
L2 := specialize TFPGList<integer>.Create;
L2.Free;
L1.Free;
end.
Now the same in Delphi mode, which is less verbose, but here a specialization to a type also has advantages:
program testspec2;
{$mode delphi}
uses fgl;
type
TMyIntegerList = TFPGlist<integer>;
var
L1:TMyIntegerList;
L2:TFPGList<integer>;
begin
{ simple and clean syntax }
L1 := TMyIntegerList.Create;
{ dirty and verbose syntax because of duplication of specialize}
L2 := TFPGList<integer>.Create;
L2.Free;
L1.Free;
end.
Now when you mix modes a lot, like I do, specialization to a type can save you a lot of work:
{ either mode delphi or objfpc here }
uses fgl;
type
{ if you switch modes the only thing in your code that
needs to change is this bit, nothing more
This for $mode delphi }
TMyIntegerList = TFPGlist<integer>;
{ into this for $mode objfpc }
TMyIntegerList = specialize TFPGList<integer>;
var
L1:TMyIntegerList;
begin
{ simple and clean syntax, all code is unaffected by the mode }
L1 := TMyIntegerList.Create;
L1.Free;
end.
I hope you now understand the adagium to specialize to a type and not a var.
I must note there are slight exceptions to this rule in very, very, very, very rare cases that you probably do not come across ever.
Specialization to a var is allowed but in 99% of all cases simply demonstrably bad programming.
The verbosity of the syntax is just historical: Freepascal had generics way before Delphi had.