[fpc-devel] Compile time functions
Thorsten Engler
thorsten.engler at gmx.net
Sun Jul 29 04:39:55 CEST 2018
type
TMyClass = class of TMyObject;
TMyObject = class
procedure InstanceMethod; //Self = TMyObject, can be virtual
class procedure ClassMethod; //Self = TMyClass, can be virtual
class procedure StaticClassMethod; static; //no Self, really just a global procedure in a scoped namespace
end;
var
o: TMyObject;
c: TMyClass;
begin
o.InstanceMethod; //if virtual, calls resolved based on actual class
o.ClassMethod; //if virtual calls, resolved based on actual class
o.StaticCalssMethod; //can’t be virtual, resolved on variable type
TMyObject.ClassMethod; //call resolved based on given type
TMyObject.StaticClassMethod; //call resolved based on given type
c.ClassMethod; //if virtual, calls resolved based on actual class
//haven’t tried if this one works, I assume it does:
c.StaticClassMethod; //can’t be virtual, resolved on variable type
end;
From: fpc-devel <fpc-devel-bounces at lists.freepascal.org> On Behalf Of J. Gareth Moreton
Sent: Sunday, 29 July 2018 07:22
To: FPC developers' list <fpc-devel at lists.freepascal.org>
Subject: Re: [fpc-devel] Compile time functions
Aah, right. Class methods I have used in past projects, but my understanding is that, in these instances, Self can still be accessed, but represents the class rather than the object, can be called without an instantiated object, and still have the issues that they can be virtual and overridden (something i'm disallowing with pure functions because of the initial complexity of determining which version of a method to call).
Can you clarify, Sven, with "static" and "class"?
Gareth aka. Kit
On Sat 28/07/18 23:11 , "Dmitry Boyarintsev" skalogryz.lists at gmail.com <mailto:skalogryz.lists at gmail.com> sent:
These are known as “class” methods (rather than “static”) in object pascal.
Thanks,
Dmitry
On Saturday, July 28, 2018, J. Gareth Moreton <gareth at moreton-family.com <javascript:top.opencompose('gareth at moreton-family.com','','','')> > wrote:
Aah, right.
I'm probably being rather dumb here from not having used them before, but do you mean things like this?
type
TNewClass = class
function Special: Boolean; static;
end;
I gather you can't access Self in such methods.
Gareth aka. Kit
On Sat 28/07/18 20:37 , "Sven Barth" pascaldragon at googlemail.com <javascript:top.opencompose('pascaldragon at googlemail.com','','','')> sent:
J. Gareth Moreton <gareth at moreton-family.com <mailto:gareth at moreton-family.com> > schrieb am Sa., 28. Juli 2018, 19:34:
I did wonder about that, but wasn't sure how it would affect things where inheritance is concerned. I dare ask, do you have an example of a class method that you would consider pure?
They don't have inheritance. If they have the modifier "static" they really are simply scoped functions or procedures. They can even be assigned to non-"of object" procedure variables.
Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180729/1b83b80c/attachment.html>
More information about the fpc-devel
mailing list