[fpc-pascal] Passing nested procs out?

2018-11-15 Thread 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  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] C-blocks

2018-11-15 Thread Sven Barth via fpc-pascal
Am Do., 15. Nov. 2018, 08:39 hat Ryan Joseph 
geschrieben:

>
>
> > On Nov 15, 2018, at 1:27 PM, Sven Barth via fpc-pascal <
> fpc-pascal@lists.freepascal.org> wrote:
> >
> > They are only useful when interacting with the external runtime. On
> other OSes nothing would support them thus there is no use for them.
> Additionally this requires extensive runtime, not to mention Objective C
> support.
> > There is even less sense in trying to port them as Object Pascal has
> anonymous functions which are already Work In Progress for FPC.
>
> I see, that’s too bad. Anonymous functions appear to be dead as far as I
> can tell. In progress since 2012 I think?
>

Unlike you I'm in active contact with the developer and the last message
was only a few months ago.


> Would it be permitted to add inline declarations of nested functions as a
> temporary replacement? They don’t capture state but at least they solve the
> issue of polluting namespace with named functions which you use in only one
> location. Better than nothing (since that’s what we’re realistically
> looking at) and no new complicated features.
>

No. This would conflict with the work of Blaise. Also even if we'd add that
now it would not make 3.2 as it would be a too invasive change. So the
earliest release would be 3.4 and for that I plan to have the real thing
integrated into trunk. Thus it would be wasted effort to add that now.

Regards,
Sven

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

Re: [fpc-pascal] Passing nested procs out?

2018-11-15 Thread Sven Barth via fpc-pascal
Am Do., 15. Nov. 2018, 10:17 hat Ryan Joseph 
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.

Regards,
Sven

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

Re: [fpc-pascal] Passing nested procs out?

2018-11-15 Thread Marcos Douglas B. Santos
On Thu, Nov 15, 2018 at 8:15 AM Sven Barth via fpc-pascal
 wrote:
>
> Am Do., 15. Nov. 2018, 10:17 hat Ryan Joseph  
> 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  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Passing nested procs out?

2018-11-15 Thread Ryan Joseph


> On Nov 15, 2018, at 5:14 PM, Sven Barth via fpc-pascal 
>  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  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Passing nested procs out?

2018-11-15 Thread Sven Barth via fpc-pascal
Am Do., 15. Nov. 2018, 12:26 hat Marcos Douglas B. Santos 
geschrieben:

> On Thu, Nov 15, 2018 at 8:15 AM Sven Barth via fpc-pascal
>  wrote:
> >
> > Am Do., 15. Nov. 2018, 10:17 hat Ryan Joseph 
> 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  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Passing nested procs out?

2018-11-15 Thread Sven Barth via fpc-pascal
Am Do., 15. Nov. 2018, 12:40 hat Ryan Joseph 
geschrieben:

>
>
> > On Nov 15, 2018, at 5:14 PM, Sven Barth via fpc-pascal <
> fpc-pascal@lists.freepascal.org> 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  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] C-blocks

2018-11-15 Thread Ryan Joseph


> On Nov 15, 2018, at 5:09 PM, Sven Barth via fpc-pascal 
>  wrote:
> 
> Unlike you I'm in active contact with the developer and the last message was 
> only a few months ago. 

That’s good news then but I’m not going to hold my breath. Best of luck to the 
man.

> 
> 
> Would it be permitted to add inline declarations of nested functions as a 
> temporary replacement? They don’t capture state but at least they solve the 
> issue of polluting namespace with named functions which you use in only one 
> location. Better than nothing (since that’s what we’re realistically looking 
> at) and no new complicated features.
> 
> No. This would conflict with the work of Blaise. Also even if we'd add that 
> now it would not make 3.2 as it would be a too invasive change. So the 
> earliest release would be 3.4 and for that I plan to have the real thing 
> integrated into trunk. Thus it would be wasted effort to add that now. 

I just went back and read some of old threads of where closure support is at 
and it was mentioned that closures (i.e. what Delphi is calling anonymous 
functions) are actually a pretty heavy weight concept and require a non-trivial 
amount of overheard. Looking at the c-blocks implementation Jonas did the and 
RTL behind it confirms this to me.

Given that, having a light-weight “anonymous nested function” (not a “reference 
to” closure) is actually a nice compliment and in fact 2 different things. As 
FPC’s current c-blocks support demonstrates, it has closure properties but NOT 
anonymous functions (yet), which are indeed 2 distance concepts. Seems to be 
both are good to have.


Regards,
Ryan Joseph

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

Re: [fpc-pascal] Passing nested procs out?

2018-11-15 Thread Ryan Joseph


> On Nov 15, 2018, at 8:24 PM, Sven Barth via fpc-pascal 
>  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  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] C-blocks

2018-11-15 Thread Sven Barth via fpc-pascal
Am Do., 15. Nov. 2018, 15:01 hat Ryan Joseph 
geschrieben:

> Given that, having a light-weight “anonymous nested function” (not a
> “reference to” closure) is actually a nice compliment and in fact 2
> different things. As FPC’s current c-blocks support demonstrates, it has
> closure properties but NOT anonymous functions (yet), which are indeed 2
> distance concepts. Seems to be both are good to have.
>

No, too many ways to do the same thing don't make things easier, not to
mention that everything needs to be maintained. So this gets a definite
"no" from me.

Also the overhead for calling an anonymous function is only that for a
virtual method call.

Regards,
Sven

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

Re: [fpc-pascal] C-blocks

2018-11-15 Thread Ryan Joseph


> On Nov 15, 2018, at 11:30 PM, Sven Barth via fpc-pascal 
>  wrote:
> 
> No, too many ways to do the same thing don't make things easier, not to 
> mention that everything needs to be maintained. So this gets a definite "no" 
> from me. 
> 
> Also the overhead for calling an anonymous function is only that for a 
> virtual method call. 

Do you mean if the anonymous function has no state it’s only a single function 
call?

After reading old forum posts it sounded like this was going to be implemented 
by some reference counted structure which was allocated on the heap and even 
using interfaces perhaps. Depending on how this is implemented it may not be 
possible to prevent them from capturing state if the anonymous function 
accesses variables in the parent scope.

How much of that is accurate?

Regards,
Ryan Joseph

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