Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2025-11

generics syntax question

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Martin Frb

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.

Reply by email

Sven Barth
to Martin Frb

Martin Frb via fpc-devel schrieb am Sa.,
29. Nov. 2025, 20:24:

Show quoted text 16 lines
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) ?

The later, please report.

Regards,
Sven

Reply by email