Passing nested procs out?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Passing nested procs out?

Ryan Joseph
Is passing nested procs outside of the calling scope relying on undefined behavior? It doesn’t seem like this should be allowed.

===========================

{$modeswitch nestedprocvars}

program test;
type
        TCallback = procedure (i: integer) is nested;


function DoTest: TCallback;
var
        context: integer = 0;

        procedure Process(x: integer);
        begin
                context += x;
                writeln(x,' ',context);
        end;
begin
        result := @Process;
end;

var
        c: TCallback;
begin
        c := DoTest(20);
        c(1);
end.


Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Passing nested procs out?

Free Pascal - General mailing list
Am Do., 15. Nov. 2018, 10:17 hat Ryan Joseph <[hidden email]> geschrieben:
Is passing nested procs outside of the calling scope relying on undefined behavior? It doesn’t seem like this should be allowed.


Don't know if it is documented as such, but if not, it should be. 

Regards, 
Sven 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Passing nested procs out?

Marcos Douglas B. Santos
On Thu, Nov 15, 2018 at 8:15 AM Sven Barth via fpc-pascal
<[hidden email]> wrote:
>
> Am Do., 15. Nov. 2018, 10:17 hat Ryan Joseph <[hidden email]> geschrieben:
>>
>> Is passing nested procs outside of the calling scope relying on undefined behavior? It doesn’t seem like this should be allowed.
>
>
> See the notes mentioned here: http://wiki.freepascal.org/FPC_New_Features_2.6.0#Support_for_nested_procedure_variables
>
> Don't know if it is documented as such, but if not, it should be.

If I understood correctly, this can be used in all cases of anonymous
functions, with the advantage to make the code more readable and
"Pascalish".
Am I right?

regards,
Marcos Douglas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Passing nested procs out?

Ryan Joseph
In reply to this post by Free Pascal - General mailing list


> On Nov 15, 2018, at 5:14 PM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>
> See the notes mentioned here: http://wiki.freepascal.org/FPC_New_Features_2.6.0#Support_for_nested_procedure_variables
>
> Don't know if it is documented as such, but if not, it should be.
>

Sorry I’m not understanding this. I read we can make the assignment but the part I’m curious about is passing the function pointer out of the nested functions scope. I did some tests and it work in some cases and gave me garbled memory in others. Just wanted to make absolutely certain what’s happening here.


Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Passing nested procs out?

Free Pascal - General mailing list
In reply to this post by Marcos Douglas B. Santos
Am Do., 15. Nov. 2018, 12:26 hat Marcos Douglas B. Santos <[hidden email]> geschrieben:
On Thu, Nov 15, 2018 at 8:15 AM Sven Barth via fpc-pascal
<[hidden email]> wrote:
>
> Am Do., 15. Nov. 2018, 10:17 hat Ryan Joseph <[hidden email]> geschrieben:
>>
>> Is passing nested procs outside of the calling scope relying on undefined behavior? It doesn’t seem like this should be allowed.
>
>
> See the notes mentioned here: http://wiki.freepascal.org/FPC_New_Features_2.6.0#Support_for_nested_procedure_variables
>
> Don't know if it is documented as such, but if not, it should be.

If I understood correctly, this can be used in all cases of anonymous
functions, with the advantage to make the code more readable and
"Pascalish".
Am I right?

No, because function references (which is the name of the variables for anonymous functions) store the captured variables in an automatically created and managed object. Nested functions simply work on the stack. So passing them outside of the context of the function they're declared in will result in garbage. 

Regards, 
Sven 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Passing nested procs out?

Free Pascal - General mailing list
In reply to this post by Ryan Joseph
Am Do., 15. Nov. 2018, 12:40 hat Ryan Joseph <[hidden email]> geschrieben:


> On Nov 15, 2018, at 5:14 PM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>
> See the notes mentioned here: http://wiki.freepascal.org/FPC_New_Features_2.6.0#Support_for_nested_procedure_variables
>
> Don't know if it is documented as such, but if not, it should be.
>

Sorry I’m not understanding this. I read we can make the assignment but the part I’m curious about is passing the function pointer out of the nested functions scope. I did some tests and it work in some cases and gave me garbled memory in others. Just wanted to make absolutely certain what’s happening here.

The linked entry answers your question directly:

"if you assign a nested routine to nested procedure variable and then exit the nested routine's parent stack frame, calling the nested procedure variable will result in undefined bahaviour [sic]." 

Regards, 
Sven 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Passing nested procs out?

Ryan Joseph


> On Nov 15, 2018, at 8:24 PM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>
> The linked entry answers your question directly:
>
> "if you assign a nested routine to nested procedure variable and then exit the nested routine's parent stack frame, calling the nested procedure variable will result in undefined bahaviour [sic]."
>

My fault for not reading more carefully. Sorry Sven!

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal