[fpc-pascal] assigning a local function to a var
David Emerson
dle3ab at angelbase.com
Thu Jul 10 21:20:56 CEST 2008
Thanks Lourival, Joao, Chris, for your responses.
It looks like what I'm attempting is not possible. FWIW, I've included a
more illustrative example at the end of this message.
Thus, I would ask of the devs, has this functionality been considered?
Is there some particular reason that anyone is opposed to implementing
it? Or is it just difficult to implement, and/or not considered useful?
There is a part of me that wants to jump in and try to implement this in
the compiler, although I suspect I would be in well over my head.
Before I consider making such an attempt (or looking for someone to do
it) it'd be nice to get a feeling for whether such an extension would
be welcome.
I am hard-pressed to detect any drawback or conflict, although of course
that's largely because I have no idea how this stuff is actually
implemented.
Lourival wrote some code which included:
> var
> my_func : TProcedure;
> begin
> my_func:= TProcedure(@func_a);
> my_func('hello, ');
> end
Lourival: while your suggestion to typecast the local function to
TProcedure did compile and run, the procedure is no longer "local"!
Thus the parent function's variables are no longer in scope, which was
the principal reason to use a local function. I neglected to include
that in my original example, so I rewrote the example below.
Chris: It seems like you're saying what I want to do is impossible.
Thanks for pointing out that citation in the reference manual. It would
be nice to hear a more definitive answer, though :) For now, I've made
the functions external, which unfortunately requires passing many
(local) parameters to them, within a loop that should go fast. :(
Chris wrote, among other things:
> This is for a reason - think what would happen if you were able to
> assign a local procedure to a global variable!
Perhaps my subject line should have read "assigning a local function to
a LOCAL var" -- that is, after all, what I'm after. ((Just because it's
possible to do something stupid with a feature, doesn't mean the
feature is a bad idea: I can think of all sorts of stupid/absurd things
to do with pointers, which compile without hint or warning.))
Joao: creating a named type isn't a solution, as it only moves the
problem of declaration into the "type" section, where the very same
problem persists. Thanks for responding, though.
On Wednesday 09 July 2008 9:31 am, David Emerson wrote:
> I'd like to store an address of a local function in a variable, and
> call that local function, but I don't know how to define a variable of
> type "local function".
Here's a revised version that better illustrates the need:
test.pas(20,14) Error: Incompatible types:
got "<address of local procedure;Register>"
expected "<procedure variable type of procedure;Register>"
procedure test;
var
what_to_say : ansistring;
procedure proc_a;
begin
writeln ('A says, "', what_to_say, '"');
end;
procedure proc_b;
begin
writeln ('B says, "', what_to_say, '"');
end;
var
my_proc : procedure; // need to specify "local". impossible?
begin
what_to_say := 'hello, world!';
my_proc := @proc_a; // won't compile! proc_a is local.
my_proc();
end;
Cheers,
~David.
More information about the fpc-pascal
mailing list