static class methods

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

static class methods

Jonathan Benedicto
Why do static class methods in FPC have a "Self" parameter? In Delphi,
static class methods do not have a Self parameter.

Jon

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

Re: static class methods

Jonas Maebe-2

On 06 Feb 2008, at 20:52, Jonathan Benedicto wrote:

> Why do static class methods in FPC have a "Self" parameter?  In  
> Delphi, static class methods do not have a Self parameter.

You need them to correctly handle cases like this:

***
{$ifdef fpc}
{$mode delphi}
{$endif}

type
   tc = class
     class procedure callme;
     class procedure test; virtual;
   end;

   td = class(tc)
     class procedure test; override;
   end;


class procedure tc.callme;
begin
   test;
end;

class procedure tc.test;
begin
   writeln('tc.test called, although td.test should be called');
end;

class procedure td.test;
begin
   writeln('td.test correctly called');
end;


var
   c: tc;
begin
   c:=td.create;
   c.callme;
   c.free;
end.

***

I'm not sure how Delphi could handle this without passing the VMT as  
invisible parameter (and from a cursory look at the code generated by  
Kylix, it does appear to pass the VMT).


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: static class methods

Jonathan Benedicto
Jonas Maebe wrote:
> I'm not sure how Delphi could handle this without passing the VMT as  
> invisible parameter (and from a cursory look at the code generated by  
> Kylix, it does appear to pass the VMT).

Delphi doesn't allow static class methods to be virtual.

Jon
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: static class methods

Micha Nelissen
Jonathan Benedicto wrote:
> Jonas Maebe wrote:
>> I'm not sure how Delphi could handle this without passing the VMT as
>> invisible parameter (and from a cursory look at the code generated by
>> Kylix, it does appear to pass the VMT).
>
> Delphi doesn't allow static class methods to be virtual.

What are "static" class methods?

"Plain" class methods are allowed to be virtual, surely.

Micha
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: static class methods

Jonathan Benedicto
Micha Nelissen:
> What are "static" class methods?

>From RAD Studio 2007's Help file on Methods
(ms-help://borland.bds5/devcommon/methods_xml.html):

Like class methods, class static methods can be accessed without an object
reference. Unlike ordinary class methods, class static methods have no Self
parameter at all. They also cannot access any instance members. (They still
have access to class fields, class properties, and class methods.) Also
unlike class methods, class static methods cannot be declared virtual.
Methods are made class static by appending the word static to their
declaration.


> "Plain" class methods are allowed to be virtual, surely.

Yes, "plain" class methods can be virtual, and can be overridden.

Jon

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

Re: static class methods

Jonas Maebe-2

On 06 Feb 2008, at 23:19, Jonathan Benedicto wrote:

>> From RAD Studio 2007's Help file on Methods
> (ms-help://borland.bds5/devcommon/methods_xml.html):
>
> Like class methods, class static methods can be accessed without an  
> object reference. Unlike ordinary class methods, class static  
> methods have no Self parameter at all. They also cannot access any  
> instance members. (They still have access to class fields, class  
> properties, and class methods.) Also unlike class methods, class  
> static methods cannot be declared virtual.
> Methods are made class static by appending the word static to their  
> declaration.

In that case the reason that FPC passes a self pointer is probably  
simply because no special support was ever added for static class  
methods (static is parsed for class methods, but I guess nothing is  
done with it afterwards). Feel free to submit a bug report.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: static class methods

Peter Vreman
In reply to this post by Jonathan Benedicto
>
> On 06 Feb 2008, at 23:19, Jonathan Benedicto wrote:
>
>>> From RAD Studio 2007's Help file on Methods
>> (ms-help://borland.bds5/devcommon/methods_xml.html):
>>
>> Like class methods, class static methods can be accessed without an
>> object reference. Unlike ordinary class methods, class static
>> methods have no Self parameter at all. They also cannot access any
>> instance members. (They still have access to class fields, class
>> properties, and class methods.) Also unlike class methods, class
>> static methods cannot be declared virtual.
>> Methods are made class static by appending the word static to their
>> declaration.
>
> In that case the reason that FPC passes a self pointer is probably
> simply because no special support was ever added for static class
> methods (static is parsed for class methods, but I guess nothing is
> done with it afterwards). Feel free to submit a bug report.

Disabling self is not possible. It is required and also used in our RTL:

     class function TObject.InstanceSize : SizeInt;
       begin
          InstanceSize:=pSizeInt(pointer(self)+vmtInstanceSize)^;
       end;


Peter


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

Re: static class methods

Jonas Maebe-2

On 07 Feb 2008, at 10:56, Peter Vreman wrote:

> Disabling self is not possible. It is required and also used in our  
> RTL:
>
>      class function TObject.InstanceSize : SizeInt;
>        begin
>           InstanceSize:=pSizeInt(pointer(self)+vmtInstanceSize)^;
>        end;

Instance size is a regular class method, not a static class method.


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