[fpc-pascal] class constants

Vinzent Hoefler JeLlyFish.software at gmx.net
Fri Jan 25 13:02:26 CET 2008


On Friday 25 January 2008 12:30, Peter Vreman wrote:
> > Regarding class constants: I missed them, too, already, although
> > not too much. ;)
>
> Maybe you should read the documentation. Static fields are supported
> already for 10 years:
>
> ~/fpc/compiler>>> cat p.pp
> {$mode objfpc}
> {$static on}
> type
>   cl=class
>     l : longint;static;
>   end;
> var
>   c1,c2 : cl;
> begin
>   c1:=cl.create;
>   c2:=cl.create;
>   c1.l:=2;
>   writeln(c2.l);
>   c2.l:=3;
>   writeln(c1.l);
> end.
> [pvreman at goofy]
> ~/fpc/compiler>>> ./p
> 2
> 3
> [pvreman at goofy]
> ~/fpc/compiler>>>

Well, once upon a time I learned that there's a difference between a 
variable and a constant, but ok; Borland might have changed that a bit 
when they introduced the notion of a "typed constant". ;)

More interesting here is that not only "c1.L" and "c2.L" works, but you 
can even access the variable without an instance at all: "cl.L", just 
like it should. So at least there are class variables.

(It might prove a bit tricky to correctly initialize them, though. 
AFAICS you can't give them initial values, can you? So to be on the 
safe side, you're probably back to doing it in the initialization 
section of the unit where the class is declared. This is a bit nasty.)

Still, something like

|cl =
|class
|   const VersionId = '1.3.9a';
|end;

seems not possible. Of course, this is easily modeled by a simple class 
method returning a constant value. That's why I said, I didn't miss 
that feature "too much".


Vinzent.



More information about the fpc-pascal mailing list