Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-10

Callbacks as nested functions

11 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ryan Joseph

Is there anyway to get callbacks to work as nested functions? I get crashes when I try this and I assume it’s because the the compiler is replacing the first parameter with another value.

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph

Am 23.10.2017 11:49 schrieb "Ryan Joseph" :

Is there anyway to get callbacks to work as nested functions? I get crashes when I try this and I assume it’s because the the compiler is replacing the first parameter with another value.

What exactly are you trying? If the nested function accesses its outer scope then it definitely won't work. For that you'd need to wait for anonymous function support (which are planned - at least a far as I'm concerned - to also support nested functions).

Regards,
Sven

Reply by email

Ryan Joseph
to Sven Barth
On Oct 23, 2017, at 4:56 PM, Sven Barth via fpc-pascal wrote: What exactly are you trying? If the nested function accesses its outer scope then it definitely won't work. For that you'd need to wait for anonymous function support (which are planned - at least a far as I'm concerned - to also support nested functions).

I cast DoThis to the type but the arguments are not correct not and a does not equal 0.

type
	TCallback = procedure (a: integer);
procedure DoCallback;
procedure DoThis(a: integer);
begin
	writeln(a);
end;
var
	callback: TCallback;
begin
	callback := TCallback(@DoThis);
	callback(100);
end;

Regards,
Ryan Joseph

Reply by email

Michael Van Canneyt
to Ryan Joseph
Ryan Joseph wrote 23 lines
> On Oct 23, 2017, at 4:56 PM, Sven Barth via fpc-pascal wrote: > > What exactly are you trying? If the nested function accesses its outer scope then it definitely won't work. For that you'd need to wait for anonymous function support (which are planned - at least a far as I'm concerned - to also support nested functions). > I cast DoThis to the type but the arguments are not correct not and a does not equal 0. type TCallback = procedure (a: integer); procedure DoCallback; procedure DoThis(a: integer); begin writeln(a); end; var callback: TCallback; begin callback := TCallback(@DoThis); callback(100); end;

You can't use a local procedure for this, as far as I know.

Michael.

Reply by email

Mattias Gaertner
to Ryan Joseph: I cast DoThis to the type but the arguments are not correct not and a does not equal 0.…
Show quoted text 6 lines
> On Oct 23, 2017, at 4:56 PM, Sven Barth via fpc-pascal wrote: > > What exactly are you trying? If the nested function accesses its outer scope then it definitely won't work. For that you'd need to wait for anonymous function support (which are planned - at least a far as I'm concerned - to also support nested functions). > I cast DoThis to the type but the arguments are not correct not and a does not equal 0.
program test1;
{$mode objfpc}{$H+}
{$ModeSwitch nestedprocvars}
type
  TCallback = procedure (a: integer) is nested;
procedure DoCallback;
  procedure DoThis(a: integer);
  begin
    writeln(a);
  end;
var
  callback: TCallback;
begin
  callback := @DoThis;
  callback(100);
end;
begin
  DoCallback;
end.

Keep in mind what Sven wrote. You cannot use "callback" outside of DoCallBack, because FPC does not yet support Delphi's "reference to".

Mattias

Reply by email

Ryan Joseph
to Mattias Gaertner
Show quoted text 28 lines
On Oct 23, 2017, at 8:55 PM, Mattias Gaertner wrote: program test1; {$mode objfpc}{$H+} {$ModeSwitch nestedprocvars} type TCallback = procedure (a: integer) is nested; procedure DoCallback; procedure DoThis(a: integer); begin writeln(a); end; var callback: TCallback; begin callback := @DoThis; callback(100); end; begin DoCallback; end. Keep in mind what Sven wrote. You cannot use "callback" outside of DoCallBack, because FPC does not yet support Delphi's "reference to".

Thanks, I never heard of nestedprocvars. Why isn’t that the default? If I have a callback I may have it be nested or not so I always want “is nested” on I guess.

Regards,
Ryan Joseph

Reply by email

Michael Van Canneyt
to Ryan Joseph
Ryan Joseph wrote 32 lines
> On Oct 23, 2017, at 8:55 PM, Mattias Gaertner wrote: > > program test1; > > {$mode objfpc}{$H+} > {$ModeSwitch nestedprocvars} > > type > TCallback = procedure (a: integer) is nested; > > procedure DoCallback; > procedure DoThis(a: integer); > begin > writeln(a); > end; > var > callback: TCallback; > begin > callback := @DoThis; > callback(100); > end; > > begin > DoCallback; > end. > > Keep in mind what Sven wrote. You cannot use "callback" outside of > DoCallBack, because FPC does not yet support Delphi's "reference to". Thanks, I never heard of nestedprocvars. Why isn’t that the default? If I have a callback I may have it be nested or not so I always want “is nested” on I guess.

It is not the default because neither Delphi nor TP allow this.

Macpas mode enables it, since it was allowed in MWP.

Michael.

Reply by email

Ryan Joseph
to Michael Van Canneyt
On Oct 23, 2017, at 9:05 PM, Michael Van Canneyt wrote: It is not the default because neither Delphi nor TP allow this.

then that begs the question, why not? :) If local functions are a useful feature then local callbacks should be too.

Regards,
Ryan Joseph

Reply by email

Michael Van Canneyt
to Ryan Joseph
Ryan Joseph wrote 7 lines
> On Oct 23, 2017, at 9:05 PM, Michael Van Canneyt wrote: > > It is not the default because neither Delphi nor TP allow this. then that begs the question, why not? :) If local functions are a useful feature then local callbacks should be too.

Well, we're not Delphi compiler developers, TP is long defunct, so this is the wrong place to ask :)

Michael.

Reply by email

LacaK
to Michael Van Canneyt

Btw there is long-time open ... may be related bug https://bugs.freepascal.org/view.php?id=18702

L.

Dňa 23.10.2017 o 16:42 Michael Van Canneyt napísal(a):

Show quoted text 21 lines
On Mon, 23 Oct 2017, Ryan Joseph wrote: > > >> On Oct 23, 2017, at 9:05 PM, Michael Van Canneyt >> wrote: >> >> It is not the default because neither Delphi nor TP allow this. > > then that begs the question, why not? :) If local functions are a > useful feature then local callbacks should be too. Well, we're not Delphi compiler developers, TP is long defunct, so this is the wrong place to ask :) Michael. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Adriaan van Os
to Ryan Joseph: then that begs the question, why not? :) If local functions are a useful feature then…
Ryan Joseph wrote 6 lines
> On Oct 23, 2017, at 9:05 PM, Michael Van Canneyt wrote: > > It is not the default because neither Delphi nor TP allow this. then that begs the question, why not? :) If local functions are a useful feature then local callbacks should be too.

Because
1. it takes a few extra processor cycles
2. it breaks binary compatibilty.

For a detailed discussion, see <https://bugs.freepascal.org/view.php?id=15925>

Regards,

Adriaan van Os

Reply by email