[fpc-pascal] "Is nested" vs "of object" compatibility with global functions
Michael Van Canneyt
michael at freepascal.org
Sun May 29 07:21:45 CEST 2022
On Sun, 29 May 2022, Hairy Pixels via fpc-pascal wrote:
> I’ve been testing out all the different function pointer types in FPC to
> test their compatibility with each other and I noticed that “is nested”
> can accept a global function declaration but “of object” can not. What is
> the reason for this exactly? I wouldn’t expect nested function types to
> accept global functions but since they do I wonder why “of object” is
> different.
Because you're pushing a parameter which does not exist in the case of a
global function.
>
> I think they both have a hidden first parameter (like self) and nested
> types simply ignore this for global functions so I would think objects
> types could do the same.
I cannot comment on the 'is nested', I don't know what the extra field is
for. I assume a parent stack frame pointer, which can perfectly be nil,
presumably.
A method pointer is actually 2 fields, both pointers:
the data (self) and the address (location of method).
Because you cannot distinguish between 'self = nil' and 'there is no self'
when actually calling the method, it's not possible to assign a global method:
you would be pushing a 'self' parameter to a procedure that is not expecting a
'self' parameter, wreaking havoc on the stack.
Theoretically one could add a third field, or maybe encode a special
value to distinguish between the two, but then you would need to add
some logic at every call of an 'of object' method to determine what is needed.
That would slow things down, and would be not be Delphi compatible.
There is plenty of code out there that assumes the current mechanism to
'just work'
Similarly, you could let all global methods accept a 'self' pointer which is
simply Nil, but same argument applies: it slows things down (extra parameter to
handle) and is not TP nor Delphi compatible.
When starting from zero, one could probably make all 3 methods (plain
procedure, 'of object' and 'is nested' compatible - maybe even
'reference to', but for historical reasons it is not possible.
Michael.
More information about the fpc-pascal
mailing list