Sign in Join

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

Mailing lists / fpc-pascal / 2012-12

Class procedural type

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Ewald

Hello,

Is there any way to define the appropriate type for `AMethod` (in mode ObjFPC)?

    TTestClass = Class
    Public
        Class Procedure AMethod;
    End;
I tried
    Type TSomeType = Class Procedure;

But this didn't work. Rather obvious of course since this could also be interpreted as a class named `Procedure` [I think].

Also I tried
    Type TSomeType = Class Procedure of Object;

But this didn't get me any further. I read the manual (section 3.6 -- http://www.freepascal.org/docs-html/ref/refse17.html) and it contains no data in respect to this little problem of mine.

Any ideas?

BTW: For time being I fixed it through
TProcType(Pointer(@TTestClass.AMethod)), where `TProcType = Procedure;`. I think this boiles down to the same as a class procedure is nothing more than a normal procedure as I see it (except for the name and some other syntax)

Ewald

Reply by email

Sven Barth
to Ewald
Ewald wrote 23 lines
Hello, Is there any way to define the appropriate type for `AMethod` (in mode ObjFPC)? TTestClass = Class Public Class Procedure AMethod; End; I tried Type TSomeType = Class Procedure; But this didn't work. Rather obvious of course since this could also be interpreted as a class named `Procedure` [I think]. Also I tried Type TSomeType = Class Procedure of Object; But this didn't get me any further. I read the manual (section 3.6 -- http://www.freepascal.org/docs-html/ref/refse17.html) and it contains no data in respect to this little problem of mine.

There is no real solution for this.

Show quoted text 8 lines
Any ideas? BTW: For time being I fixed it through TProcType(Pointer(@TTestClass.AMethod)), where `TProcType = Procedure;`. I think this boiles down to the same as a class procedure is nothing more than a normal procedure as I see it (except for the name and some other syntax)

This is not correct. A class method without static still has a hidden parameter through which the class type is passed. If you want to use the above you need to declare your method as "static;". In that case a method will behave the same as a normal procedure variable, but if you want to change e.g. class properties or class variables of the class the method belongs to, you must use the class name (e.g. TMyClass.MyProperty) to access it.

Regards,
Sven

Reply by email

Ewald
to Sven Barth
Sven Barth wrote
There is no real solution for this.

I already thought so, I thank you for clearing this matter up to me.

Show quoted text 11 lines
> Any ideas? > > BTW: For time being I fixed it through > TProcType(Pointer(@TTestClass.AMethod)), where `TProcType = Procedure;`. > I think this boiles down to the same as a class procedure is nothing > more than a normal procedure as I see it (except for the name and some > other syntax) > This is not correct. A class method without static still has a hidden parameter through which the class type is passed.
Ah, this is interesting. I didn't know of this hidden parameter, but now that you mention it, it makes sense. So if I declare the method as
	Class Procedure AMethod; static;

It should not contain this hidden parameter? In that case the above code (with the intermediate pointer cast) would prove correct? (of course it is no cross platform solution or anything, but that is besides the matter now)

If you want to use the above you need to declare your method as "static;". In that case a method will behave the same as a normal procedure variable, but if you want to change e.g. class properties or class variables of the class the method belongs to, you must use the class name (e.g. TMyClass.MyProperty) to access it.

So if I get you right, the use of `static` after a method make the hidden class type parameter disappear, but also mandates use of the class name every time I want to use the class' [static fields / class properties / class methods]?

Suppose TTestClass contains `AField: Integer; static;`, then the following would be right if I understand you correctly?

Class Procedure TTestClass.AMethod; static;  // I don't know if `static` should be in the implementation as well, but I'll soon figure out :-)
Begin
	AField:= 0;		//Wrong? My guess is it would fail to compile in this case, but I haven't got a compiler at hand now.
	TTestClass.AField:= 0;	//Right?
End;

Sorry if I've just written the same thing twice (maybe thrice ;-) ) , but I've got this feeling that this isn't anywhere in the manual pages so I want to be absolutely sure about this.

Ewald

Reply by email

Paul Ishenin
to Ewald

18.12.12, 5:26, Ewald пишет:

Show quoted text 8 lines
Suppose TTestClass contains `AField: Integer; static;`, then the following would be right if I understand you correctly? Class Procedure TTestClass.AMethod; static; // I don't know if `static` should be in the implementation as well, but I'll soon figure out :-) Begin AField:= 0; //Wrong? My guess is it would fail to compile in this case, but I haven't got a compiler at hand now. TTestClass.AField:= 0; //Right? End;

Both calls will work. AMethod still belongs to class, so it first searches identifier in that class.

Best regards,
Paul Ishenin

Reply by email

Sven Barth
to Paul Ishenin
"Paul Ishenin" wrote 5 lines
18.12.12, 5:26, Ewald пишет: > Suppose TTestClass contains `AField: Integer; static;`, then the

following would be right if I understand you correctly?

> > Class Procedure TTestClass.AMethod; static; // I don't know if `static`

should be in the implementation as well, but I'll soon figure out :-)

> Begin > AField:= 0; //Wrong? My guess is it would fail to

compile in this case, but I haven't got a compiler at hand now.

Show quoted text 6 lines
> > TTestClass.AField:= 0; //Right? > End; Both calls will work. AMethod still belongs to class, so it first

searches identifier in that class.

Right... Forgot about the scope... -.-

Regards,
Sven

Reply by email

Sven Barth
to Ewald: I already thought so, I thank you for clearing this matter up to me. Ah, this is…
"Ewald" wrote 14 lines
On 17 Dec 2012, at 20:04, Sven Barth wrote: > > There is no real solution for this. I already thought so, I thank you for clearing this matter up to me. > >> Any ideas? >> >> BTW: For time being I fixed it through >> TProcType(Pointer(@TTestClass.AMethod)), where `TProcType =

Procedure;`.

Show quoted text 6 lines
>> I think this boiles down to the same as a class procedure is nothing >> more than a normal procedure as I see it (except for the name and some >> other syntax) >> > > This is not correct. A class method without static still has a hidden

parameter through which the class type is passed.

Ah, this is interesting. I didn't know of this hidden parameter, but now

that you mention it, it makes sense. So if I declare the method as

Class Procedure AMethod; static; It should not contain this hidden parameter? In that case the above code

(with the intermediate pointer cast) would prove correct? (of course it is no cross platform solution or anything, but that is besides the matter now)

It would be a cross plattform solution, because the method pointer will just be a Pointer insteat of a TMethod record.

> If you want to use the above you need to declare your method as

"static;". In that case a method will behave the same as a normal procedure variable, but if you want to change e.g. class properties or class variables of the class the method belongs to, you must use the class name (e.g. TMyClass.MyProperty) to access it.

So if I get you right, the use of `static` after a method make the hidden

class type parameter disappear, but also mandates use of the class name every time I want to use the class' [static fields / class properties / class methods]?

Not quite, see Paul's mail. Sorry for the confusion :)

Suppose TTestClass contains `AField: Integer; static;`, then the

following would be right if I understand you correctly?

Class Procedure TTestClass.AMethod; static; // I don't know if `static`

should be in the implementation as well, but I'll soon figure out :-)

It's enough to have it in the declaration.

Regards,
Sven

Reply by email

Ewald
to Sven Barth

Once upon a time, on 12/18/2012 06:42 AM to be precise, Sven Barth said:

Show quoted text 12 lines
Am 17.12.2012 22:27 schrieb "Ewald" < >: > > It should not contain this hidden parameter? In that case the above code (with the intermediate pointer cast) would prove correct? (of course it is no cross platform solution or anything, but that is besides the matter now) It would be a cross plattform solution, because the method pointer will just be a Pointer insteat of a TMethod record.

Indeed, my thoughts got a bit mixed up at one point. This would indeed be cross platform.

Show quoted text 23 lines
> > If you want to use the above you need to declare your method as "static;". In that case a method will behave the same as a normal procedure variable, but if you want to change e.g. class properties or class variables of the class the method belongs to, you must use the class name (e.g. TMyClass.MyProperty) to access it. > > So if I get you right, the use of `static` after a method make the hidden class type parameter disappear, but also mandates use of the class name every time I want to use the class' [static fields / class properties / class methods]? > Not quite, see Paul's mail. Sorry for the confusion :) > Suppose TTestClass contains `AField: Integer; static;`, then the following would be right if I understand you correctly? > > Class Procedure TTestClass.AMethod; static; // I don't know if `static` should be in the implementation as well, but I'll soon figure out :-) It's enough to have it in the declaration.

Alright. Seems like you helped me fix this issue. Thanks for your time (as well as Paul's of course)!

Ewald

Reply by email