Recent

Author Topic: Why is it impossible to call not static object class method by object type  (Read 9006 times)

zamtmn

  • Hero Member
  • *****
  • Posts: 594
There are objective reasons for this?
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. type
  3.   TMyType=object
  4.     class procedure MyMethod;
  5.   end;
  6. class procedure TMyType.MyMethod;
  7. begin
  8.   writeln('class procedure TMyType.MyMethod');
  9. end;
  10. begin
  11.   TMyType.MyMethod;
  12.   readln;
  13. end.
Code: [Select]
project1.lpr(11,19) Error: Only static methods and static variables can be referenced through an object type

ASerge

  • Hero Member
  • *****
  • Posts: 2223
project1.lpr(11,19) Error: Only static methods and static variables can be referenced through an object type
Select bold. Use class.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
You are calling TMyType.MyMethod as if it were a classic procedure.

Yet there is no object instance in existence that the non-static MyMethod's hidden Self parameter can refer to.
Declaring the class procedure MyMethod as static removes that hidden Self parameter from the object's class method so you can call it as if it were a classic non-method procedure, i.e. a procedure not associated with any individual object.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Code: [Select]
project1.lpr(11,19) Error: Only static methods and static variables can be referenced through an object type

You need to change 'object' to 'class'.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. type
  4.   TMyType=class // <-- HERE
  5.     class procedure MyMethod;
  6.   end;
  7.  
  8. class procedure TMyType.MyMethod;
  9. begin
  10.   writeln('class procedure TMyType.MyMethod');
  11. end;
  12.  
  13. begin
  14.   TMyType.MyMethod;
  15.   readln;
  16. end.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

zamtmn

  • Hero Member
  • *****
  • Posts: 594
I understand the difference between object and class. In this case, it would be better for me to use object. But I don't understand why a difference here, it seems to me that nothing should interfere such a construction

engkin

  • Hero Member
  • *****
  • Posts: 3112
You can still use an object just add static to the end.

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Basically this is because class and object have slightly different feature sets. Classes itself are not only types but also itself dataobjects usable at runtime. So when calling a class function that is non static you pass a self parameter pointing to the dataobjects.

Objects simply do not have this feature and therefore class functions must be static

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
There are objective reasons for this?

Yes, because non-static class methods have a Self parameter that contains the class type on which you invoke the method. However TP-style objects don't have the this concept of a class type and thus you can't call non-static class methods on a TP-style object. Ideally the compiler should also forbid that one can declare them and only allow the static ones...

zamtmn

  • Hero Member
  • *****
  • Posts: 594
@engkin
I want to understand the reasons

@Warfley
These are just general phrases

@PascalDragon
We either have a more productive Object. Or we prohibit everything up to the TP level (including static and class methods). The first option is much better))
>>Yes, because non-static class methods have a Self parameter
We can pass NIL we are guaranteed not to use this in class methods. It work in class
« Last Edit: October 19, 2021, 09:18:22 am by zamtmn »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
@PascalDragon
We either have a more productive Object. Or we prohibit everything up to the TP level (including static and class methods). The first option is much better))

No, we won't have a “more productive Object”. It's mainly there for TP-compatibility and maybe the one or other use case that can't be fullfilled by advanced records nowadays. If you need more functionality, use class.

zamtmn

  • Hero Member
  • *****
  • Posts: 594
Re: Why is it impossible to call not static object class method by object type
« Reply #10 on: October 19, 2021, 09:27:45 am »
Then you need to declare the object as deprecated. How it is done in delphi. The apparent appearance of new features (which as it turns out are a bug) misleads people

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: Why is it impossible to call not static object class method by object type
« Reply #11 on: October 19, 2021, 09:36:00 am »
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. type
  3.   test = object
  4.   procedure testme;static;
  5.   end;
  6.  
  7.   procedure test.testme;
  8.   begin
  9.     writeln('succeeded');
  10.   end;
  11. begin
  12.   Test.testme;
  13. end.
Huh?

Nothing to do with deprecated, it is just a slightly different syntax.
Maybe the other answers already hinted at that, but this example is proof:
Note I do not instantiate anything and use "object"....
This is the "object" equivalent to class methods...
« Last Edit: October 19, 2021, 09:44:12 am by Thaddy »
Specialize a type, not a var.

zamtmn

  • Hero Member
  • *****
  • Posts: 594
Re: Why is it impossible to call not static object class method by object type
« Reply #12 on: October 19, 2021, 09:38:20 am »
>>Huh?
no
Code: Pascal  [Select][+][-]
  1. ...
  2. class procedure testme;//static;
  3. ...
  4. Test.testme;
why not?

If we say "A", we should also say "B"


« Last Edit: October 19, 2021, 09:40:38 am by zamtmn »

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: Why is it impossible to call not static object class method by object type
« Reply #13 on: October 19, 2021, 09:48:37 am »
object <> class
Specialize a type, not a var.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Why is it impossible to call not static object class method by object type
« Reply #14 on: October 19, 2021, 09:51:48 am »
Then you need to declare the object as deprecated. How it is done in delphi. The apparent appearance of new features (which as it turns out are a bug) misleads people

Is this new code, or are you able to demonstrate that FPC is not behaving in a way documented by TP and Delphi?

The core developers repeat on a regular basis that _object_ is only there for legacy compatibility, and all new work is going into _class_. If the rest of us don't like that then I'm sure they'd appreciate compiler patches.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018