class property accessor static

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

class property accessor static

Mattias Gaertner
Hi,

The getter/setter of a class-property must be "static" (Delphi
compatible).
If I understand "static" correctly, then "static" simply omits passing
the class as parameter. So a static class procedure is just a dumber
version of a normal class procedure.

What is the advantage of using "static" for class property accessors?
Aka: Why did Delphi choose static here?


Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Maciej Izak

2017-02-07 12:10 GMT+01:00 Mattias Gaertner <[hidden email]>:
The getter/setter of a class-property must be "static" (Delphi
compatible).
If I understand "static" correctly, then "static" simply omits passing
the class as parameter. So a static class procedure is just a dumber
version of a normal class procedure.

What is the advantage of using "static" for class property accessors?
Aka: Why did Delphi choose static here?

Generally "static" means no hidden parameter "self". "static" for methods is used for methods designed for callbacks from external API. 

We have 3 possibilities:

===code begin===
type
  TFooClass = class of TFoo; 
  TFoo = class
    function A: Int32; // self as instance
    class function B: Int32; // self as meta class of TFoo
    class function C: Int32; static; // no self

    property D: Int32 read A; 
    property E: Int32 read B; 
    class property F: Int32 read C;
===code end===

"class property" has two advantages. First: you can use "static" methods for properties and second: will work for improper implemented code ;) (which is maybe disadvantage?):

TFoo($1).F;
TFooClass($1).F;

--
Best regards,
Maciej Izak

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Mattias Gaertner
On Tue, 7 Feb 2017 12:30:26 +0100
Maciej Izak <[hidden email]> wrote:

> 2017-02-07 12:10 GMT+01:00 Mattias Gaertner <[hidden email]>:
>[...]
> Generally "static" means no hidden parameter "self". "static" for methods
> is used for methods designed for callbacks from external API.

That's an advantage of static methods.
But how is that related to class properties?
How can you use class properties with "methods designed for
callbacks from external API"?

 

> We have 3 possibilities:
>
> ===code begin===
> type
>   TFooClass = class of TFoo;
>   TFoo = class
>     function A: Int32; // self as instance
>     class function B: Int32; // self as meta class of TFoo
>     class function C: Int32; static; // no self
>
>     property D: Int32 read A;
>     property E: Int32 read B;
>     class property F: Int32 read C;
> ===code end===
>
> "class property" has two advantages. First: you can use "static" methods
> for properties

Sorry, I don't get it.
Why is using a static method as accessor an advantage?


> and second: will work for improper implemented code ;)
> (which is maybe disadvantage?):
>
> TFoo($1).F;
> TFooClass($1).F;

I don't see why these calls require static instead of normal.

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Maciej Izak

2017-02-07 13:11 GMT+01:00 Mattias Gaertner <[hidden email]>:
That's an advantage of static methods.
But how is that related to class properties?
How can you use class properties with "methods designed for
callbacks from external API"?

Is not related. AFAIK "class property" exist for complexity of language (otherwise usage of "static" methods would be impossible for "property" syntax).
 
Sorry, I don't get it.
Why is using a static method as accessor an advantage?

You can use Get/Set pair of static methods in "property" form. Might be more handy.
 
> and second: will work for improper implemented code ;)
> (which is maybe disadvantage?):
>
> TFoo($1).F;
> TFooClass($1).F;

I don't see why these calls require static instead of normal.

In other scenario (for normal "property") this call will raise AV.

btw. "class property" is probably slightly faster ;) because don't pass "self" parameter.

--
Best regards,
Maciej Izak

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Michael Van Canneyt


On Tue, 7 Feb 2017, Maciej Izak wrote:

>> I don't see why these calls require static instead of normal.
>>
>
> In other scenario (for normal "property") this call will raise AV.
>
> btw. "class property" is probably slightly faster ;) because don't pass
> "self" parameter.

In other words, there is no real reason why the method must be static.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Mattias Gaertner
In reply to this post by Maciej Izak
On Tue, 7 Feb 2017 13:25:16 +0100
Maciej Izak <[hidden email]> wrote:

> 2017-02-07 13:11 GMT+01:00 Mattias Gaertner <[hidden email]>:
>[...]
> > Sorry, I don't get it.
> > Why is using a static method as accessor an advantage?
> >  
>
> You can use Get/Set pair of static methods in "property" form. Might be
> more handy.

Why is that "more handy"? "static" does not have a Self. That
is less handy, isn't it?


> > > and second: will work for improper implemented code ;)
> > > (which is maybe disadvantage?):
> > >
> > > TFoo($1).F;
> > > TFooClass($1).F;  
> >
> > I don't see why these calls require static instead of normal.
>
> In other scenario (for normal "property") this call will raise AV.

I can't follow you here. Are we still talking about why Delphi choose
static instead of normal?

 
> btw. "class property" is probably slightly faster ;) because don't pass
> "self" parameter.

True.
OTOH you loose some possibilities.

Mattias
 
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Maciej Izak

2017-02-07 13:51 GMT+01:00 Mattias Gaertner <[hidden email]>:
Why is that "more handy"? "static" does not have a Self. That
is less handy, isn't it?

by "handy" I mean usage of "class properties" instead of "static methods".
 
I can't follow you here. Are we still talking about why Delphi choose
static instead of normal?

"class property" is used for code where class instance (nor assign to meta class) is not needed. Some variance/idea of singleton pattern. TFoo might be used as namespace:

===code begin===
var
  Foo: TFooClass; // or Foo: TFoo
begin
  WriteLn(Foo.F);
end;
===code end===

for regular "property" with "class methods" you need something like this:

===code begin===
var
  Foo: TFooClass;
begin
  Foo := TFoo;
  WriteLn(Foo.E);
end;
===code end===

and for normal property:

===code begin===
var
  Foo: TFoo;
begin
  Foo := TFoo.Create;
  WriteLn(Foo.D);
end;
===code end===
 
True.
OTOH you loose some possibilities.

True.

--
Best regards,
Maciej Izak

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Graeme Geldenhuys-6
On 2017-02-07 13:19, Maciej Izak wrote:

> "class property" is used for code where class instance (nor assign to meta
> class) is not needed. Some variance/idea of singleton pattern. TFoo might
> be used as namespace:
>
> ===code begin===
> var
>   Foo: TFooClass; // or Foo: TFoo
> begin
>   WriteLn(Foo.F);
> end;
> ===code end===


Couldn't that simply be....

  Writeln(TFoo.F);


???

I never understood Object Pascal's class methods/properties either?
Unlike Java, not everything needs to be in a class. We are allowed to
have procedures or functions, and global ones at that. So in Object
Pascal we could simply have a unit called FooStuff where everything
related to Foo is defined, then have a global function F() and then use
it as follows...

  Writeln(F);


Regards,
  Graeme

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Mattias Gaertner
On Tue, 7 Feb 2017 13:31:10 +0000
Graeme Geldenhuys <[hidden email]> wrote:

>[...]
> Couldn't that simply be....
>
>   Writeln(TFoo.F);

Yes.


> I never understood Object Pascal's class methods/properties either?
> Unlike Java, not everything needs to be in a class. We are allowed to
> have procedures or functions, and global ones at that. So in Object
> Pascal we could simply have a unit called FooStuff where everything
> related to Foo is defined, then have a global function F() and then use
> it as follows...

Class methods can be overridden.

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Maciej Izak

2017-02-07 14:35 GMT+01:00 Mattias Gaertner <[hidden email]>:
> I never understood Object Pascal's class methods/properties either?
> Unlike Java, not everything needs to be in a class. We are allowed to
> have procedures or functions, and global ones at that. So in Object
> Pascal we could simply have a unit called FooStuff where everything
> related to Foo is defined, then have a global function F() and then use
> it as follows...

Class methods can be overridden.

not static. btw. class property is not my idea :P. 

Don't forget that you can use property as array and is possible to use index keyword.

type
  TFoo = class
    class function F(i: integer): byte; static;
    class property P1: byte index 10 read F;
    class property P2[idx: integer]: byte read F;
  end;

--
Best regards,
Maciej Izak

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Graeme Geldenhuys-6
In reply to this post by Mattias Gaertner
On 2017-02-07 13:35, Mattias Gaertner wrote:
> Class methods can be overridden.

But then you would have to define a new type first. In that case you can
simply have a instance variable too. Personally, it just doesn't make
much sense to me in Object Pascal.

But thanks for clarifying the difference.


Regards,
  Graeme

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Sven Barth-2
In reply to this post by Graeme Geldenhuys-6

Am 07.02.2017 14:31 schrieb "Graeme Geldenhuys" <[hidden email]>:
> I never understood Object Pascal's class methods/properties either?
> Unlike Java, not everything needs to be in a class. We are allowed to
> have procedures or functions, and global ones at that. So in Object
> Pascal we could simply have a unit called FooStuff where everything
> related to Foo is defined, then have a global function F() and then use
> it as follows...

It allows for grouping. This way one can directly see (by using the completion window or by peeking at the declaration oneself) that there is a routine F() that belongs somehow to TFoo. If you put that into a separate unit however then it isn't clear by itself that the stuff relates to TFoo.
In the end it's a question of taste. I personally like class methods and class properties.
Oh and don't forget that non-static class methods can be virtual (sometimes I really miss that in C++).

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Mattias Gaertner
On Tue, 7 Feb 2017 19:23:52 +0100
Sven Barth <[hidden email]> wrote:

>[...]
> Oh and don't forget that non-static class methods can be virtual (sometimes
> I really miss that in C++).

In FPC static class methods can be virtual as well.

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Maciej Izak

2017-02-07 19:49 GMT+01:00 Mattias Gaertner <[hidden email]>:
In FPC static class methods can be virtual as well.

So we have bug...

--
Best regards,
Maciej Izak

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Sven Barth-2

Am 07.02.2017 19:59 schrieb "Maciej Izak" <[hidden email]>:
>
>
> 2017-02-07 19:49 GMT+01:00 Mattias Gaertner <[hidden email]>:
>>
>> In FPC static class methods can be virtual as well.
>
>
> So we have bug...

Indeed...

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Mattias Gaertner
On Tue, 7 Feb 2017 23:20:18 +0100
Sven Barth <[hidden email]> wrote:

> Am 07.02.2017 19:59 schrieb "Maciej Izak" <[hidden email]>:
> >
> >
> > 2017-02-07 19:49 GMT+01:00 Mattias Gaertner <[hidden email]>:  
> >>
> >> In FPC static class methods can be virtual as well.  
> >
> >
> > So we have bug...  
>
> Indeed...

A useful bug.

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Paul Ishenin
In reply to this post by Mattias Gaertner
07.02.2017 18:10, Mattias Gaertner wrote:
> The getter/setter of a class-property must be "static" (Delphi
> compatible).
> If I understand "static" correctly, then "static" simply omits passing
> the class as parameter. So a static class procedure is just a dumber
> version of a normal class procedure.
>
> What is the advantage of using "static" for class property accessors?
> Aka: Why did Delphi choose static here?
Class properties has apperared together with class constants and class
variables (which are static by their nature). They were introduced (as I
understand) to give access to private/protected static elements.

--
Best regards,
Paul Ishenin.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class property accessor static

Luiz Americo Pereira Camara-3


2017-02-07 23:17 GMT-03:00 Paul Ishenin <[hidden email]>:
07.02.2017 18:10, Mattias Gaertner wrote:
The getter/setter of a class-property must be "static" (Delphi
compatible).
If I understand "static" correctly, then "static" simply omits passing
the class as parameter. So a static class procedure is just a dumber
version of a normal class procedure.

What is the advantage of using "static" for class property accessors?
Aka: Why did Delphi choose static here?
Class properties has apperared together with class constants and class variables (which are static by their nature). They were introduced (as I understand) to give access to private/protected static elements.


For the record, there was a discussion about the topic years ago: https://www.mail-archive.com/fpc-devel@.../msg30511.html


Luiz

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Loading...