[fpc-pascal] Class procedural type

Ewald ewald at yellowcouch.org
Mon Dec 17 22:26:21 CET 2012


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;`.
>> 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




More information about the fpc-pascal mailing list