[fpc-devel] generics syntax question
Martin Frb
lazarus at mfriebe.de
Sat Nov 29 20:24:49 CET 2025
It is possible in const (anywhere where a const can be given, not just
const blocks) to be able to:
- access values inside structures
- take the address of a field.
That also works for types created by specialisation. But only if they
are specialized to a new named type.
specialization can also (in most places) be used "in place".
Are the below errors
- intentional, and meant to stay
- result of an oversight (and fixed, or considered for being fixed if
ever time allows for it)
?
program Project1;{$Mode objfpc}
type
TFoo = class
public f: integer; g: word;
public const c = 1;
end;
generic TGen<X> = class
public f: X; g: word;
public const c = 1;
end;
TBar = specialize TGen<byte>;
const
a1 = TFoo.c;
a2 = ptruint(@TFoo(nil).g);
b1 = TBar.c;
b2 = ptruint(@TBar(nil).g);
// next 2 give errro
c1 = specialize TGen<byte>.c;
c2 = ptruint(@ specialize TGen<byte>(nil).g);
var
t: ptruint;
begin
// same syntax => works:
t := specialize TGen<byte>.c;
t := ptruint(@ specialize TGen<byte>(nil).g);
end.
More information about the fpc-devel
mailing list