* * *

Author Topic: Static directive in Class, Virtual Final  (Read 1263 times)

darksky666

  • New member
  • *
  • Posts: 38
Static directive in Class, Virtual Final
« on: March 27, 2018, 11:41:35 am »
From design point, why do you think was static directive not allowed in normal class methods, it's allowed in objects but not in classes, was it deemed redundant just because you can use class procedure anyway?

Code: Pascal  [Select]
  1. type
  2.   cl = class
  3.    i : integer; static; (* static directive on class fields allowed *)
  4.  
  5.     procedure testProc; static; (* Static directive not allowed on methods *)
  6.   end;
  7.  

Does anyone have an idea why?
Thanks
« Last Edit: March 27, 2018, 01:19:21 pm by darksky666 »

Thaddy

  • Hero Member
  • *****
  • Posts: 6880
Re: Static directive in Class
« Reply #1 on: March 27, 2018, 12:24:09 pm »
No. they are different beasts (depending on mode)
https://www.freepascal.org/docs-html/ref/refsu30.html more specific:
"In the implementation of a static class method, the Self identifier is not available. The method behaves as if Self is hardcoded to the declared class, not the actual class with which it was called. In regular class methods, Self contains the Actual class for which the method was called."

If you want the class procedure to behave like a regular procedure or function then you need the static modifier. Otherwise a class procedure does not necessarily behave like a normal procedure. But that depends on mode.

On objects, same story except that you do not need the class prefix.
In old school syntax you can write:
Code: Pascal  [Select]
  1. {$mode tp}
  2. type
  3.   cl = object  // TP mode does not know classes, just objects.
  4.    i : integer; static; (* static directive on class fields allowed *)  // <--- Yes, in delphi, object pascal and TP modes
  5.      procedure testProc; static; (* Static directive not allowed on methods *) // Well: no. they are allowed in old style syntax, but not in classes: needs a class method
  6.   end;
  7. begin
  8. end.

Full example:
Code: Pascal  [Select]
  1. {$mode objfpc}
  2. type
  3.   Tcl = class
  4.    i : integer; static;
  5.     class procedure testProc1; static;
  6.     class procedure testProc2;
  7.   end;
  8. { this will fail: identifier not found "Self"}
  9.   class procedure Tcl.testproc1;
  10.   begin
  11.     Writeln(Self.Classname);
  12.   end;
  13. { but this writes the classname }  
  14.   class procedure Tcl.testproc2;
  15.   begin
  16.     Writeln(self.Classname);
  17.   end;
  18. var
  19.   cl:Tcl;
  20. begin
  21.   Tcl.Testproc2;
  22. end.

You need to comment out the Testproc1 for this to compile.
See the difference?


« Last Edit: March 27, 2018, 12:37:32 pm by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

darksky666

  • New member
  • *
  • Posts: 38
Re: Static directive in Class
« Reply #2 on: March 27, 2018, 01:18:21 pm »
@Thaddy, i am thankful that you went into great detail for explaining the concept I want to ask this too:

(I found out this thing even exists from Delphi 10.2 Documentation, I tried it in fpc (in objfpc mode), and it compiled fine!... doesn't marking a method as final defeat the whole purpose of marking it as virtual in the first place?

Why does fp even allow virtual; final; directive in class? If it's not going to be allowed to be overridden then what's the purpose fo marking it as virtual at all?

Code: Pascal  [Select]
  1. type
  2.   cl = class
  3.     procedure proc; virtual; final;
  4.   end;
  5.  
 
« Last Edit: March 27, 2018, 01:29:39 pm by darksky666 »

Thaddy

  • Hero Member
  • *****
  • Posts: 6880
Re: Static directive in Class, Virtual Final
« Reply #3 on: March 27, 2018, 01:29:26 pm »
The static modifier is not in any way redundant (as per my example). And objects are not classes. I will see if I have the time to write another example. Right now I am stuck at regexpr  :'(
Also note the virtual modifier is with classes only allowed for root classes. In any derived class the modifier is override.
As opposed to objects, that do not know about override, only about virtual.
The reason that you can declare a method virtual and sealed is both a parser issue, an exact syntax issue and a language issue: virtual implies a VMT (virtual method table). There are cases where you want to access the VMT directly, even if the method is sealed, so no longer can be modified. This is specifically important when using inline assembler code.
« Last Edit: March 27, 2018, 01:32:08 pm by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

darksky666

  • New member
  • *
  • Posts: 38
Re: Static directive in Class, Virtual Final
« Reply #4 on: March 27, 2018, 01:32:07 pm »
Oh, I am sorry, I felt the whole question was bit unnecessary so i edited it out but turns out you already replied before i pushed that save button.

Anyway, Thank you man..

Thaddy

  • Hero Member
  • *****
  • Posts: 6880
Re: Static directive in Class, Virtual Final
« Reply #5 on: March 27, 2018, 01:33:43 pm »
Did it help you? Otherwise I still will write an example. It is an important subject for intermediate and advanced programmers, so I won't mind.
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

darksky666

  • New member
  • *
  • Posts: 38
Re: Static directive in Class, Virtual Final
« Reply #6 on: March 27, 2018, 01:51:54 pm »
Ah, thank you, but I am not doing anything specific, just came across that virtual final method thing and asked about it, an example would be great but you can do it when you're free, or (since it's an important concept) i will ask about it in the forum in future.. 
« Last Edit: March 27, 2018, 02:09:05 pm by darksky666 »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus