Sign in Join

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

Mailing lists / fpc-pascal / 2003-04

class variable

9 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

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

milimeter

Hello, everybody

Can any tell me if there's "Class Variable" in free pascal like in java? I mean, in java, you can declare a class memeber to be "static", all instance of this class share the same variable memory. Such a member can be accessed through class name without any instantiation. Does free pascal will add such a feature in the future?

Thanks
mili

Reply by email

Peter Vreman
to Anton Tichawa: Hello! AFAIK, class variables are not supported yet, nor are class properties. But it's…
Show quoted text 14 lines
Hello! > Can any tell me if there's "Class Variable" in free pascal like in > java? > I mean, in java, you can declare a class memeber to be "static", all > instance of this class share the same variable memory. Such a member can > be > accessed through class name without any instantiation. > Does free pascal will add such a feature in the future? AFAIK, class variables are not supported yet, nor are class properties. But it's possible to use class methods, which in turn access a global variable.

Sorry that is wrong, static class members are supported, you need to enable the static keyword using -St

I don't know what you mean with class properties.

-

Reply by email

Anton Tichawa
to milimeter: Hello, everybody Can any tell me if there's "Class Variable" in free pascal like in java?…

Hello!

Show quoted text 5 lines
Can any tell me if there's "Class Variable" in free pascal like in java? I mean, in java, you can declare a class memeber to be "static", all instance of this class share the same variable memory. Such a member can be accessed through class name without any instantiation. Does free pascal will add such a feature in the future?

AFAIK, class variables are not supported yet, nor are class properties. But it's possible to use class methods, which in turn access a global variable.

hth,

Anton.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Ing. Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
phone: +43 1 218 97 59
mobil: +43 664 52 07 907; currently n/a
email:

----------

Reply by email

milimeter

Great!
But does -St also contain the parameters of -S2/-Sd? Or can we use them at them at the same time? "fpc -St -Sd aaa.pp"?

Show quoted text 6 lines
Sorry that is wrong, static class members are supported, you need to enable the static keyword using -St I don't know what you mean with class properties.

Reply by email

Peter Vreman
to Anton Tichawa: Sorry for my misinfo, but I didn't find anything about class variables in the docs…
Show quoted text 12 lines
When trying class methods, I found that the executable behaves strange: Invoking a class method with <classtype>.<method> works fine. But, invoking the class method with <classvar>.<method> is accepted by the compiler, but results in an exception when <classvar> is nil. Is that OK?

Please try it with 1.0.7 or 1.1. There are some changes regarding class methods since 1.0.6

Reply by email

Michael Van Canneyt
to Anton Tichawa: Sorry for my misinfo, but I didn't find anything about class variables in the docs…
Anton Tichawa wrote 23 lines
On Friday 04 April 2003 07:48, you wrote: > > Hello! > > > >> Can any tell me if there's "Class Variable" in free pascal like in > >> java? > >> I mean, in java, you can declare a class memeber to be "static", all > >> instance of this class share the same variable memory. Such a member can > >> be > >> accessed through class name without any instantiation. > >> Does free pascal will add such a feature in the future? > > > > AFAIK, class variables are not supported yet, nor are class properties. > > But > > it's possible to use class methods, which in turn access a global > > variable. > > Sorry that is wrong, static class members are supported, you need to > enable the static keyword using -St > > I don't know what you mean with class properties. Sorry for my misinfo, but I didn't find anything about class variables in the docs (1.06), and I tried, in vain, to compile programs with class variables.

I think peter was wrong. He meant class functions or methods.

Show quoted text 20 lines
I'm happy to learn that -St allows using class variables. It's a pity, though, that different keywords have to be used for class variables and class methods. By class properties, I mean properties that only access class members (class variables and class methods), and are accessible with the syntax: <classtype>.<propertyname>. When trying class methods, I found that the executable behaves strange: Invoking a class method with <classtype>.<method> works fine. But, invoking the class method with <classvar>.<method> is accepted by the compiler, but results in an exception when <classvar> is nil. Is that OK?

It depends on what you do in this method. It definitely works, because e.g. 'Classname' is a class method.

The idea of a class variable is strange to me.
Why not simply use a global variable ?

Michael.

Reply by email

Anton Tichawa
to Peter Vreman: Sorry that is wrong, static class members are supported, you need to enable the static…
you wrote 19 lines
> Hello! > >> Can any tell me if there's "Class Variable" in free pascal like in >> java? >> I mean, in java, you can declare a class memeber to be "static", all >> instance of this class share the same variable memory. Such a member can >> be >> accessed through class name without any instantiation. >> Does free pascal will add such a feature in the future? > > AFAIK, class variables are not supported yet, nor are class properties. > But > it's possible to use class methods, which in turn access a global > variable. Sorry that is wrong, static class members are supported, you need to enable the static keyword using -St I don't know what you mean with class properties.

Sorry for my misinfo, but I didn't find anything about class variables in the docs (1.06), and I tried, in vain, to compile programs with class variables. I'm happy to learn that -St allows using class variables. It's a pity, though, that different keywords have to be used for class variables and class methods.

By class properties, I mean properties that only access class members (class variables and class methods), and are accessible with the syntax:

<classtype>.<propertyname>.

When trying class methods, I found that the executable behaves strange: Invoking a class method with

<classtype>.<method>

works fine. But, invoking the class method with

<classvar>.<method>

is accepted by the compiler, but results in an exception when <classvar> is nil. Is that OK?

thanks,

anton tichawa.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Ing. Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
phone: +43 1 218 97 59
mobil: +43 664 52 07 907; currently n/a
email:

----------

Reply by email

Anton Tichawa
to Michael Van Canneyt: I think peter was wrong. He meant class functions or methods. It depends on what you do…

Hello!

The idea of a class variable is strange to me. Why not simply use a global variable ?

In fact it would be a global variable, but one that is bound to a class, also in the sense of a namespace.

Consider, as an example, a class variable that holds the count of instances. Something like (this is pseudo code - it will not work that simple):

type t_myclass_a = class
  class var instance_count: integer;
  end;
type t_my_class_b = class
  class var instance_count: integer;
  end;
procedure list_instances(aclass: tclass);
begin
  writeln('the class ' + aclass.classname + ' has ' +
    intstr(aclass.instance_count) + ' instances');
  // i. e. accessing the proper global variable for
  // the given class type
end;

Type checking and inheritance of class variables still would have to be considered.

Anton.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Ing. Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
phone: +43 1 218 97 59
mobil: +43 664 52 07 907; currently n/a
email:

----------

Reply by email