Re: [fpc-pascal] "Is nested" vs "of object" compatibility with global functions

2022-05-29 Thread Hairy Pixels via fpc-pascal


> On May 29, 2022, at 2:26 PM, Martin Frb via fpc-pascal 
>  wrote:
> 
> Actually afaik you push an extra param in for "is nested" as well as for "of 
> object.
> 
> But for "of object" it is the first param, for "is nested" it is the last.

So you can cast nested or object to each other and the compiler doesn’t 
complain the parameters have bad values so this confirms what you’re saying.

Anyways the lesson learned today playing with these is that we still shouldn’t 
just make all our function pointers “reference to” because each time you add 
one of these anonymous functions in a function it will add some setup code at 
runtime, even if it’s not called. If you’re doing something realtime the 
penalty will add up fast and be a real problem if you’re not careful.

Luckily anonymous functions are available on the other types so we can use 
those without incurring runtime penalties but we still can’t write 
caller-agnostic functions which have no runtime penalty and accept global, 
nested or object function pointers.

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] "Is nested" vs "of object" compatibility with global functions

2022-05-29 Thread Hairy Pixels via fpc-pascal


> On May 29, 2022, at 2:26 PM, Martin Frb via fpc-pascal 
>  wrote:
> 
> Actually afaik you push an extra param in for "is nested" as well as for "of 
> object.
> 
> But for "of object" it is the first param, for "is nested" it is the last.

Then “references to” is the only universal function pointer type now (which is 
great) but since it’s a heap constructed class it’s about 4 times slower than 
the other ones. I still think FPC needs a universal stack based function 
pointer type which works with objects, nested and global functions. Michael 
hinted to how that could be done but it sounds like this all legacy baggage 
from Delphi.

Ideally the programmer only has to consider “does it leave the scope or not” 
and not have to worry about these other 2 is nested/of object types. I can’t 
even think of any other language that has so many function pointer types which 
are visible to the programmer (even c++???).

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] "Is nested" vs "of object" compatibility with global functions

2022-05-29 Thread Martin Frb via fpc-pascal

On 29/05/2022 07:21, Michael Van Canneyt via fpc-pascal wrote:



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.


Actually afaik you push an extra param in for "is nested" as well as for 
"of object.


But for "of object" it is the first param, for "is nested" it is the last.

procedure TFoo.Bar(self: TBar; a: integer);
procedure NestedBar(a: integer; parentFp: pointer);

So in the code at the end of the mail, with a global function assigned 
to "p", it calls

  p(1, SomeValueForParentFp).
But the global "procedure f" just ignores the extra param.

If it was "of object", the "self" value would be first, and it would be 
in place of the parameter "a".



program Foo;
type TProc = procedure(a:integer) is nested;
var  p: TProc;

procedure f(a:integer);
begin
end;

begin
  p:= @f;
  p(1);
end.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] "Is nested" vs "of object" compatibility with global functions

2022-05-28 Thread Michael Van Canneyt via fpc-pascal



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.___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal