Recent

Author Topic: Global Property, Static Class Method  (Read 2831 times)

darksky666

  • New Member
  • *
  • Posts: 38
Global Property, Static Class Method
« on: March 22, 2018, 01:51:50 pm »
1. Why does Global Property (outside of any Class) not accept a variable as setter or getter? Is there any reason why it specifically needs function/procedure instead?

2. From REF.PDF (6.5.6 - Static Class Methods)
Quote
They can be assigned to regular procedural variables.

Code: Pascal  [Select][+][-]
  1.  
  2. type
  3.   cl = class
  4.     nv : integer;
  5.     class var
  6.     cv : integer;
  7.  
  8.     procedure np;
  9.     class procedure cp;
  10.     class procedure scp; static;
  11.   end;    
  12.  
  13. var
  14.   i : char;
  15.   c : cl;
  16.   nproc : procedure;
  17.   cproc : procedure of object;
  18.  
  19.  
  20. begin
  21.   c := cl.create;
  22.   cproc := @c.np;
  23.  
  24.   (* Both of the following result Error *)
  25.   cproc := @cl.scp;
  26.   nproc := @cl.scp;
  27.  
  28.  c.Free;
  29. end.
  30.  

So what was the Reference Manual actually trying to say?

Thanks

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Global Property, Static Class Method
« Reply #1 on: March 22, 2018, 05:59:36 pm »
1. Why does Global Property (outside of any Class) not accept a variable as setter or getter? Is there any reason why it specifically needs function/procedure instead?
Global property purpose is to hide the implementation. And if you use a global variable, then it must already be somewhere explicitly defined above, so there is already a direct access to this variable and the property is not needed.
Quote
2. From REF.PDF (6.5.6 - Static Class Methods)
Quote
They can be assigned to regular procedural variables.
So what was the Reference Manual actually trying to say?
I think it's a error. The code is compiled using {$MODE DELPHI} or {$MODESWITCH CLASSICPROCVARS} (of course, without @).

darksky666

  • New Member
  • *
  • Posts: 38
Re: Global Property, Static Class Method
« Reply #2 on: March 23, 2018, 01:33:26 am »
Quote
Global property purpose is to hide the implementation. And if you use a global variable, then it must already be somewhere explicitly defined above, so there is already a direct access to this variable and the property is not needed.

Wow, that makes sense, it's very well thought out..

 
Quote
I think it's a error. The code is compiled using {$MODE DELPHI} or {$MODESWITCH CLASSICPROCVARS}

I rechecked it with the delphi mode and that switch, it still didn't compile, so i guess you're right about it being an error..

Also, i forgot to ask it before but is there any way of having multiple variant parts in a record (Other than doing the below)?

Code: Pascal  [Select][+][-]
  1.   rec = record
  2.     a : integer;
  3.     b : char;
  4.     c : word;
  5.  
  6.     variantPart1 : record
  7.         case opt: word of
  8.            1: (abc : LongInt;);
  9.            2: (def : LongWord;);
  10.            3: (fgh : array[0..3] of char;);
  11.         end;
  12.  
  13.       case variantPart2 : word of
  14.         1: (ghj: array[0..7] of char;);
  15.         2: (jkl: array[0..1] of Longint;);
  16.   end;
  17.  

I have to go through variantPart1 before i can access it's data, is there any alternative way to solve this?

Thank you..

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Global Property, Static Class Method
« Reply #3 on: March 23, 2018, 08:13:11 am »
Variant parts can be multi, but first:
Code: Pascal  [Select][+][-]
  1. program Test2;
  2. {$mode objfpc} //<<<------- If you want to use the monkey syntax...
  3. type
  4.   cl = class
  5.     nv : integer;
  6.     class var
  7.     cv : integer;
  8.  
  9.     procedure np;
  10.     class procedure cp;
  11.     class procedure scp; static;
  12.   end;    
  13.  
  14. var
  15.   i : char;
  16.   c : cl;
  17.   nproc : procedure;
  18.   cproc : procedure of object; // this is wrong for class procedures or and/or static
  19.  
  20.  
  21. begin
  22.   c := cl.create;
  23.   cproc := @c.np; // Monkey syntax
  24.   (* Both of the following result Error *) // WRONG!
  25.   cproc := @cl.scp;  // this one does
  26.   nproc := @cl.scp;  // this one doesn't
  27.   c.Free;
  28. end.
Or:
Code: Pascal  [Select][+][-]
  1. program Test2;
  2. {$mode delphi} //<<<------- If you do not want to use the monkey syntax...
  3. type
  4.   cl = class
  5.     nv : integer;
  6.     class var
  7.     cv : integer;
  8.  
  9.     procedure np;
  10.     class procedure cp;
  11.     class procedure scp; static;
  12.   end;    
  13.  
  14. var
  15.   i : char;
  16.   c : cl;
  17.   nproc : procedure;
  18.   cproc : procedure of object; // this is *wrong* for class procedures or and/or static assignments
  19.  
  20.  
  21. begin
  22.   c := cl.create;
  23.   cproc := c.np; // No Monkey syntax
  24.   (* Both of the following result Error *) // WRONG!
  25.   cproc := cl.scp;  // this one does, because it is NOT a procedure of object
  26.   nproc := cl.scp;  // this one doesn't
  27.   c.Free;
  28. end.

Will give an example on your last issue later.

Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Global Property, Static Class Method
« Reply #4 on: March 23, 2018, 08:22:33 am »
https://www.freepascal.org/docs-html/current/ref/refsu15.html seems clear to me.
The syntax you used compiles and is also correct, but note you can also nest variant parts.
I am not sure I quite understand your issue: the record syntax is quite clear.
Specialize a type, not a var.

darksky666

  • New Member
  • *
  • Posts: 38
Re: Global Property, Static Class Method
« Reply #5 on: March 23, 2018, 09:25:05 am »
I copied the code but it still not compiling for me (Laz 1.8.2 - FPC 3.0.4 on Xubuntu):

Quote
project1.lpr(26,12) Error: Incompatible types: got "<class method type of procedure of object;Register>" expected "<procedure variable type of procedure;Register>"

Screenshot


Okay, I tested it with {$modeswitch classicprocvars}, it compiles and works fine in objfpc mode (but without @ operator on assignment statement).

It worked fine both with and without that modeswitch in delphi mode this time, I must have messed up something in my previous attempt after reply from @ASerge, it hadn't compiled back then even with delphi mode.
« Last Edit: March 24, 2018, 02:16:32 am by darksky666 »

 

TinyPortal © 2005-2018