Sign in Join

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

Mailing lists / fpc-devel / 2008-10

RFE: Static virtual variables

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

Alexander Klenin

Currently this code:

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

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.

Reply by email

Thaddy
to Alexander Klenin
Alexander Klenin wrote 6 lines
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.

Hallvard Vassbotn (and others) has published about the same idea (with implementation) for Delphi. Should work for FPC too, but is a nice to have on the language level.
See
http://hallvards.blogspot.com/2007/05/hack17-virtual-class-variables-part-i.html and the second part as well as his original reference.

No virus found in this outgoing message.
Checked by AVG - http://www.avg.com
Version: 8.0.173 / Virus Database: 270.8.1/1728 - Release Date: 16-10-2008 7:38

Reply by email