Lazarus

Free Pascal => General => Topic started by: zamtmn on October 18, 2021, 10:27:06 pm

Title: Why is it impossible to call not static object class method by object type
Post by: zamtmn on October 18, 2021, 10:27:06 pm
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
Title: Re: Why is it impossible to call not static object class method by object type
Post by: ASerge on October 18, 2021, 11:29:08 pm
project1.lpr(11,19) Error: Only static methods and static variables can be referenced through an object type
Select bold. Use class.
Title: Re: Why is it impossible to call not static object class method by object type
Post by: howardpc on October 18, 2021, 11:33:41 pm
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.
Title: Re: Why is it impossible to call not static object class method by object type
Post by: Remy Lebeau on October 19, 2021, 02:04:07 am
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.
Title: Re: Why is it impossible to call not static object class method by object type
Post by: zamtmn on October 19, 2021, 06:24:00 am
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
Title: Re: Why is it impossible to call not static object class method by object type
Post by: engkin on October 19, 2021, 08:09:41 am
You can still use an object just add static to the end.
Title: Re: Why is it impossible to call not static object class method by object type
Post by: Warfley on October 19, 2021, 08:35:31 am
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
Title: Re: Why is it impossible to call not static object class method by object type
Post by: PascalDragon on October 19, 2021, 08:55:22 am
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...
Title: Re: Why is it impossible to call not static object class method by object type
Post by: zamtmn on October 19, 2021, 09:15:00 am
@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
Title: Re: Why is it impossible to call not static object class method by object type
Post by: PascalDragon on October 19, 2021, 09:19:48 am
@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.
Title: Re: Why is it impossible to call not static object class method by object type
Post by: zamtmn 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
Title: Re: Why is it impossible to call not static object class method by object type
Post by: Thaddy 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...
Title: Re: Why is it impossible to call not static object class method by object type
Post by: zamtmn 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"


Title: Re: Why is it impossible to call not static object class method by object type
Post by: Thaddy on October 19, 2021, 09:48:37 am
object <> class
Title: Re: Why is it impossible to call not static object class method by object type
Post by: MarkMLl 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
Title: Re: Why is it impossible to call not static object class method by object type
Post by: Thaddy on October 19, 2021, 10:10:04 am
Mark,
object is deprecated for almost 20 years. I already suggested to the Delphi people to withdraw that "deprecated" many moons ago since it has definite and applicable functionality other than class. I also suggested on this forum in a different context that for the same reason the "deprecated" mark should be removed.
Title: Re: Why is it impossible to call not static object class method by object type
Post by: Thaddy on October 19, 2021, 12:12:07 pm
I almost know:
- Devs will drop either object  or deprecated on object.
The deprecated has been misinformation for many years.
Title: Re: Why is it impossible to call not static object class method by object type
Post by: MarkMLl on October 19, 2021, 12:22:34 pm
I almost know:
- Devs will drop either object  or deprecated on object.
The deprecated has been misinformation for many years.

We are repeatedly told that he core developers don't like object. We are also repeatedly told that TP and Delphi compatibility are extremely important.

My own no-doubt-uninformed opinion is that so much effort has been involved in keeping up with Delphi's advances that that compatibility will not be discarded, even if- and I'm not trying to criticise here- the result would be a cleaner language.

MarkMLl
Title: Re: Why is it impossible to call not static object class method by object type
Post by: korba812 on October 19, 2021, 12:31:31 pm
I can use class function for objects like 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. var
  11.   O: TMyType;
  12. begin
  13.   O.MyMethod;
  14.   readln;
  15. end.
Title: Re: Why is it impossible to call not static object class method by object type
Post by: kupferstecher on October 19, 2021, 12:34:19 pm
The core developers repeat on a regular basis that _object_ is only there for legacy compatibility, and all new work is going into _class_.

Yes, but thats really a pity. On target embedded I use OOP without a dynamic memory.

In my opinion class and object should be rectified to go hand in hand with each other. On syntax level nothing would change only the internals like the VMT could change. That would mean full backward compatibility on code level. Internally object and class could be managed in the same way, for the class type this would mean that there should be a more lightweight base class other than TObject and VMT being optional. If there is a very lightweight object and class type, having no overhead, i.e. internally behaving like a record, then even the record type could be seen as deprecated.

I guess what stands against it, are concerns, that Delphi does future changes that may be incompatible to such a fpc-specific implementation.
Title: Re: Why is it impossible to call not static object class method by object type
Post by: MarkMLl on October 19, 2021, 12:40:19 pm
Yes, but thats really a pity.

Yes, it /is/ a pity... but the solution is to fork the project. And quite frankly I think there are far more fundamental problems in Pascal which need to be fixed if that is done, based on "best practice" which has become obvious in the 50 years since Wirth hacked it.

None of which detracts from FPC/Lazarus's position as one of if not /the/ best development environment of this type.

MarkMLl
Title: Re: Why is it impossible to call not static object class method by object type
Post by: Thaddy on October 19, 2021, 01:05:04 pm
I can use class function for objects like this:
You are declaring a var in your code or something. That defeats the point of this discussion. As per MY example.
Class syntax is simply different from object syntax.....(sigh)

Really: object is not class, class procedures on objects are unreasonable in the first place.
Just need static to have the same result....
Title: Re: Why is it impossible to call not static object class method by object type
Post by: PascalDragon on October 19, 2021, 01:37:29 pm
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

No, deprecated means that no further work is done and that even bugs aren't fixed. However we do fix bugs (as seldom as they are for object) and here and then a feature implemented for classes falls off for objects as well simply because they share a lot of code in the compiler.

We are repeatedly told that he core developers don't like object. We are also repeatedly told that TP and Delphi compatibility are extremely important.

Please don't put words in our mouths. We don't say that we “don't like object”. It's simply that we see no real use to invest in extending it. object has its uses (it's even used inside the compiler in some locations, though nowadays they could be replaced with advanced records) like the one mentioned by kuperstecher on embedded platforms, but that's it. It's simply that the type is not going to grow beyond its original aspirations.

In my opinion class and object should be rectified to go hand in hand with each other. On syntax level nothing would change only the internals like the VMT could change. That would mean full backward compatibility on code level. Internally object and class could be managed in the same way, for the class type this would mean that there should be a more lightweight base class other than TObject and VMT being optional. If there is a very lightweight object and class type, having no overhead, i.e. internally behaving like a record, then even the record type could be seen as deprecated.

They are too different and there is code out there that depends on these differences (yes, even the VMT, because the class vmt is available as System.TVMT (https://freepascal.org/docs-html/current/rtl/system/tvmt.html) and the object one is also documented (https://freepascal.org/docs-html/current/prog/progsu165.html#x209-2230008.2.12)). So not going to happen.
Title: Re: Why is it impossible to call not static object class method by object type
Post by: zamtmn on October 19, 2021, 02:20:34 pm
I don't remember class methods and static methods in TP, maybe in latest BP?
In my opinion, it should be banned class methods or enable referenced by type

I understand that a lot of effort is being spent, and other things are in priority... But everything should work logically

>>Just need static to have the same result....
not quite - static not compatible with "of object"
Title: Re: Why is it impossible to call not static object class method by object type
Post by: Thaddy on October 19, 2021, 03:44:24 pm
class vars were as a syntazx never in TP. Only later Delphi.
Title: Re: Why is it impossible to call not static object class method by object type
Post by: zamtmn on October 19, 2021, 04:15:31 pm
class vars were as a syntazx never in TP. Only later Delphi.
what abount static methods?
Title: Re: Why is it impossible to call not static object class method by object type
Post by: korba812 on October 19, 2021, 04:47:03 pm
You are declaring a var in your code or something. That defeats the point of this discussion. As per MY example.
Class syntax is simply different from object syntax.....(sigh)

Really: object is not class, class procedures on objects are unreasonable in the first place.
Just need static to have the same result....
Right, but the question is why compiler allows incorrect object definition.
Title: Re: Why is it impossible to call not static object class method by object type
Post by: PascalDragon on October 20, 2021, 09:13:52 am
Right, but the question is why compiler allows incorrect object definition.

Due to objects and classes sharing quite a lot of code inside the compiler it needs to be actively forbidden for one and if that is forgotten, well, that leads to situations like this.
Title: Re: Why is it impossible to call not static object class method by object type
Post by: zamtmn on October 20, 2021, 05:22:40 pm
Still, it's probably better to fix class methods for objects. this is a very useful thing

edit:
I also want to clarify: Is normal work of class constructors and class destructor for objects an bug?
Title: Re: Why is it impossible to call not static object class method by object type
Post by: PascalDragon on October 21, 2021, 09:08:23 am
Still, it's probably better to fix class methods for objects. this is a very useful thing

There is nothing that can be fixed, because non-static class methods simply can not work for objects. Objects have no TClass-like meta type which is required for that. The only fix there'll be is to disallow non-static class methods for objects, cause they simply don't make sense.

I also want to clarify: Is normal work of class constructors and class destructor for objects an bug?

Class constructors and destructors are not supposed to work on objects either.
Title: Re: Why is it impossible to call not static object class method by object type
Post by: zamtmn on October 21, 2021, 10:23:38 am
PascalDragon
Ok. thanks!
Title: Re: Why is it impossible to call not static object class method by object type
Post by: creaothceann on October 25, 2021, 08:55:19 am
[...] 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.

Why is the static keyword necessary anyway? Ideally the class keyword in front of procedure or function should already remove the hidden parameter.
Title: Re: Why is it impossible to call not static object class method by object type
Post by: PascalDragon on October 25, 2021, 09:30:10 am
Why is the static keyword necessary anyway? Ideally the class keyword in front of procedure or function should already remove the hidden parameter.

Because for classes it makes a difference whether it's declared with static or not. For objects the correct usage - since they predate the class keyword - is procedure Name; static;. That the class keyword can be used as well is simply a remnant of classes and objects essentially sharing the same parser code.
TinyPortal © 2005-2018