[fpc-pascal] FPC class syntax was extended to support delphi code
Paul Ishenin
webpirat at mail.ru
Thu Jan 14 02:23:42 CET 2010
David Emerson wrote:
> a. 'var' sections -- I assume that 'var' is optional when declaring
> fields that occur first (i.e. directly after "private", "public", etc)
>
Yes.
> b. What does "strict private" mean, as opposed to private without
> strict? (My best guess is that it would be accessible only within the
> methods+properties of that class within the unit, as opposed to being
> available anywhere in the same unit where the class is declared...???)
>
"strict private" is not a new feature. it means that something is
private for this class only and can't be accessed by other classes or
routines of the unit.
> c. What is the purpose of a class method? It would seem to me that
> methods should do the same thing whether they are class methods or not.
> Traditional variables change from instance to instance, but methods do
> not vary between instances, as far as I know-- right?
>
Class method is not a new feature as well as static class method. Class
method allows you to have methods which belongs to the class instead of
an instance of the class. Class methods can be virtual. Static class
methods can't.
> d. What happens with inheritance?
>
> d.1. wrt class constants and class vars-- are there separate "instances"
> (for lack of a better word) of these, one instance for each descendant?
> Or is the class var/const only stored once for the ancestor that
> declares it, and all descendants share that?
>
Class vars (static class fields) are stored only once for the class
which declares them and all descendants share that.
Example:
TSomeClass = class
class var Value: Integer;
end;
TDescendant = class(TSomeClass);
begin
TSomeClass.Value := 1;
TDescendant.Value := 2;
WriteLn(TSomeClass.Value); // this must output 2
end;
Class static field is not a new feature. But now you can declare them
using 'class var' section after methods or 'type'/'const' section.
> d.2. wrt class methods, can they be virtual? (This strikes me as being
> closely related to d.1)
>
class methods can be virtual. static class methods can't.
> I guess an example is in order for d:
>
> type
> t_parent = class
> private
> class var
> x : integer;
> const
> c = 5;
> end;
>
> t_child = class (t_parent)
> private
> const
> c = 7; // can I do this?
> end;
>
In ObjFpc mode you can't do this because t_child.c duplicates
t_parent.c. In Delphi mode you can do this. I have no personal opinion
how ObjFpc mode should work here but this behavior is consistent with
restrictions for identifier duplication we currently have in objfpc mode.
> e. Is it available in {$mode objfpc}? (I hope I hope I hope)?
>
yes.
Best regards,
Paul Ishenin.
More information about the fpc-pascal
mailing list