[fpc-pascal] Generic type parameter variable initialization
Sven Barth
pascaldragon at googlemail.com
Thu Jan 5 14:19:40 CET 2012
Am 05.01.2012 11:57, schrieb kyan:
> Hello everybody.
>
> I am new to FPC/Lazarus and I am trying to port some Delphi XE code
> that uses the units Generics.Defaults and Generics.Collections so I
> have to write these units for FPC because they do not exist and the
> code I am porting relies heavily on them. In this process I have come
> across various issues with Delphi/FPC generic compatibility but I have
> managed to overcome most of them.
If there are compatibility problems that are not yet mentioned in the
bugtracker at http://bugs.freepascal.org/ it would be nice if you'd
report them.
>
> In a method of a generic class I need to initialize a variable of type
> "T" (the generic class type parameter) with the type T's "default"
> value, which is 0 for ordinal types, '' for strings, nil for
> pointers/classes/interfaces, Unassigned for Variants etc. In Delphi
> there is the "compiler magic" function Default() and you can write:
>
> procedure TMyClass<T>.SomeMethod;
> var
> V: T;
> begin
> ...
> V := Default(T);
> ...
> end;
>
> This does not compile in FPC. Is there an alternative in FPC for the
> function Default()? I suppose one could do this:
This functionality does not exist yet. See also here:
http://bugs.freepascal.org/view.php?id=9420
> procedure TMyClass<T>.SomeMethod;
> var
> V: T;
> begin
> ...
> Finalize(V);
> FillChar(V, SizeOf(V), 0);
> ...
> end;
>
> which is equivalent but it doesn't look too elegant. I am using FPC
> 2.7.1 for Win32/64.
For now you'll need to use this workaround solution.
Regards,
Sven
More information about the fpc-pascal
mailing list