1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
{ default can be used with generic type parameters as well }
program tdefault9;
{$mode objfpc}
type
generic TTest<T> = class
f: T;
constructor Create;
end;
{ TTest }
constructor TTest.Create;
begin
f := Default(T);
end;
type
TLongIntSpez = specialize TTest<LongInt>;
TAnsiStringSpez = specialize TTest<AnsiString>;
TObjectSpez = specialize TTest<TObject>;
var
si: TLongIntSpez;
sa: TAnsiStringSpez;
so: TObjectSpez;
begin
si := TLongIntSpez.Create;
if si.f <> 0 then
Halt(1);
sa := TAnsiStringSpez.Create;
if sa.f <> '' then
Halt(2);
so := TObjectSpez.Create;
if so.f <> Nil then
Halt(3);
end.
|