[fpc-devel] RFE: Static virtual variables

Alexander Klenin klenin at gmail.com
Wed Oct 15 16:24:10 CEST 2008

Currently this code:

  T1 = class
    FStat: Integer; static;
  T2 = class(T1);
  T1.FStat := 5;
  T2.FStat := 6;
  Writeln(T1.FStat, T2.FStat);

Outputs '66'. That is (I think) as designed, but is IMO unfortunate ;-)
I'd like to propose a language extension where
adding 'virtual' modifier to a static field declaration, like this:

    FStat: Integer; static; virtual;

Creates one instance of the variable per class, including derived classes.
To implement it, compiler might either reserve a VMT slot for a pointer
to the variable instance or just assign a fixed offset from the VMT.

This would be very useful, for example, when creating object persistency
frameworks. Currently to associate a unique data instance with every
class I have to create a global hash table using class name as a key --
clearly not optimal solution.

Alexander S. Klenin
Insight Experts Ltd.

More information about the fpc-devel mailing list