Forum > General

Strange capture denial and acceptance for Anonymous Functions and Function refs

<< < (2/2)

Mr.Madguy:

--- Quote from: PascalDragon on September 28, 2022, 09:08:42 am ---Not quite. There is no concept of “procedure of interface”, instead a “reference to procedure” is turned into an interface with a single method Invoke. You can see this, because you can manually implement a function reference in a custom class just like an ordinary interface or you can even inherit from it.

--- End quote ---
Problem is - it isn't right. Why? Because TCapturer can have several Invoke methods. Because closures aren't only about making callbacks with data bound to them. They're about declaring classes without actually declaring classes. Because they're thing from scripted languages, that lack explicit class declaration syntax. And therefore reference to procedure/function should also store pointer to specific method - not pointer to interface only. Therefore it's something like "procedure/function of interface".

Sorry, checked it in Delphi - indeed each reference to procedure/function is separate interface, so no pointer to specific procedure/function is needed.

Example:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TMyMethod = reference to procedure;  TMyClass = record    MyInc:TMyMethod;    MyDec:TMyMethod;    MyWrite:TMyMethod;  end; function ConstructClass:TMyClass;  var X:Integer;begin  X := 0;  Result.MyInc := procedure begin Inc(X); end;  Result.MyDec := procedure begin Dec(X); end;  Result.MyWrite := procedure begin WriteLn(X); end;end; 

PascalDragon:

--- Quote from: Mr.Madguy on September 28, 2022, 09:41:01 am ---
--- Quote from: PascalDragon on September 28, 2022, 09:08:42 am ---Not quite. There is no concept of “procedure of interface”, instead a “reference to procedure” is turned into an interface with a single method Invoke. You can see this, because you can manually implement a function reference in a custom class just like an ordinary interface or you can even inherit from it.

--- End quote ---
Problem is - it isn't right. Why? Because TCapturer can have several Invoke methods. Because closures aren't only about making callbacks with data bound to them. They're about declaring classes without actually declaring classes. Because they're thing from scripted languages, that lack explicit class declaration syntax. And therefore reference to procedure/function should also store pointer to specific method - not pointer to interface only. Therefore it's something like "procedure/function of interface".

Sorry, checked it in Delphi - indeed each reference to procedure/function is separate interface, so no pointer to specific procedure/function is needed.
--- End quote ---

You think we - or mainly the one who did most of the work regarding function references - didn't do our homework to find out what's going on behind the scenes? ;D

Mr.Madguy:

--- Quote from: PascalDragon on September 28, 2022, 01:44:30 pm ---You think we - or mainly the one who did most of the work regarding function references - didn't do our homework to find out what's going on behind the scenes? ;D

--- End quote ---
May be my memory fools me or may be it's from older Delphi versions, like 2009. But I always thought, that I investigated it long time ago and that it worked exactly this way.

Bogen85:

--- Quote from: PascalDragon on September 28, 2022, 09:08:42 am ---I had not thought about that use case, but in essence it should be possible as well, considering that it works when assigning a nested function. Please do a feature request on our bug tracker.

--- End quote ---

Done. See https://gitlab.com/freepascal.org/fpc/source/-/issues/39926

PascalDragon:

--- Quote from: Mr.Madguy on September 28, 2022, 09:09:56 pm ---
--- Quote from: PascalDragon on September 28, 2022, 01:44:30 pm ---You think we - or mainly the one who did most of the work regarding function references - didn't do our homework to find out what's going on behind the scenes? ;D

--- End quote ---
May be my memory fools me or may be it's from older Delphi versions, like 2009. But I always thought, that I investigated it long time ago and that it worked exactly this way.

--- End quote ---

More likely your memory fools you, because the Delphi behaviour didn't change between versions. ;)


--- Quote from: Bogen85 on September 29, 2022, 02:10:22 am ---
--- Quote from: PascalDragon on September 28, 2022, 09:08:42 am ---I had not thought about that use case, but in essence it should be possible as well, considering that it works when assigning a nested function. Please do a feature request on our bug tracker.

--- End quote ---

Done. See https://gitlab.com/freepascal.org/fpc/source/-/issues/39926

--- End quote ---

Thank you. At least it won't be forgotten now. :)

Navigation

[0] Message Index

[*] Previous page

Go to full version