Sign in Join

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

Mailing lists / fpc-devel / 2018-01

Static class methods can or cannot be virtual?

6 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Denis Kozlov

Hello,

Can static class methods be virtual?

According to the FPC documentation, static class methods cannot be virtual, as stated here:
https://www.freepascal.org/docs-html/ref/refsu30.html

But in practice, FPC compiles and executes virtual and overridden static class methods just fine.

The following code prints "AB" with FPC 3.0.4 and 2.6.4.

=======================================
type
   TTestA = class
   protected
     class function GetName: String; virtual; static;
   public
     class property Name: String read GetName;
   end;
type
   TTestB = class(TTestA)
   protected
     class function GetName: String; override; static;
   end;
class function TTestA.GetName: String;
begin
   Result := 'A';
end;
class function TTestB.GetName: String;
begin
   Result := 'B';
end;
begin
   WriteLn(TTestA.Name, TTestB.Name);
end.
=======================================

Thanks,
Denis

Reply by email

Denis Kozlov
to Maciej Izak
Maciej Izak wrote
This was bug, fixed in trunk (see my rev. 35724)

Thank you for pointing that out.

I presume then there is no way for having a class property who's returned value can be dynamically changed in child classes? In other words, class property getters/setters can only be implemented by the class that declared the property.

Thanks,
Denis

Reply by email

Maciej Izak
to Denis Kozlov

2018-01-22 18:25 GMT+01:00 Denis Kozlov :

Show quoted text 5 lines
I presume then there is no way for having a class property who's returned value can be dynamically changed in child classes? In other words, class property getters/setters can only be implemented by the class that declared the property.

Correct. There was some topic about "static class methods" and "class property" some time ago on fpc-devel or in fpc-pascal. For example static class methods can be used as callback procedure for external APIs/Libraries.

Best regards, Maciej Izak

Reply by email

Mattias Gaertner
to Maciej Izak
Show quoted text 11 lines
2018-01-22 18:25 GMT+01:00 Denis Kozlov : > I presume then there is no way for having a class property who's returned > value can be dynamically changed in child classes? In other words, class > property getters/setters can only be implemented by the class that declared > the property. > Correct. There was some topic about "static class methods" and "class property" some time ago on fpc-devel or in fpc-pascal. For example static class methods can be used as callback procedure for external APIs/Libraries.

Can you explain what external APIs/Libraries have to do with class properties?

Mattias

Reply by email

Maciej Izak
to Mattias Gaertner

2018-01-22 18:48 GMT+01:00 Mattias Gaertner :

Show quoted text 10 lines
On Mon, 22 Jan 2018 18:45:57 +0100 Maciej Izak wrote: > Correct. There was some topic about "static class methods" and "class > property" some time ago on fpc-devel or in fpc-pascal. For example static > class methods can be used as callback procedure for external APIs/Libraries. Can you explain what external APIs/Libraries have to do with class properties?

"Class property" was not mentioned in the part about "external APIs/Libraries".

Best regards, Maciej Izak

Reply by email