[fpc-pascal] assigning a local function to a var

Lourival Mendes mendes.lourival at gmail.com
Wed Jul 9 19:05:41 CEST 2008


I'm not sure, but you could use TProcedure, some think like this:

procedure test;

   function func_a (pass_str : ansistring) : boolean;
      begin
         writeln (pass_str, 'A');
         func_a := true;
      end;

   function func_b (pass_str : ansistring) : boolean;
      begin
         writeln (pass_str, 'B');
         func_b := false;
      end;

   var
      // TProcedure
      my_func : TProcedure
      result: boolean ;

   begin
     my_func:= nil ;

     if not Assigned(my_func) then begin

        my_func:= TProcedure(@func_a);
        result:= my_func('hello, ');
   end
   else begin .....

    end;

end;

Try it and let me know...


Lourival Mendes

2008/7/9 David Emerson <dle3ab at angelbase.com>:
> 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 the error I'm stuck on, with sample code:
>
> Error: Incompatible types:
> got "<address of local function(AnsiString):Boolean;Register>"
> expected "<procedure variable type of
> function(AnsiString):Boolean;Register>"
>
>
> procedure test;
>
>   function func_a (pass_str : ansistring) : boolean;
>      begin
>         writeln (pass_str, 'A');
>         func_a := true;
>      end;
>
>   function func_b (pass_str : ansistring) : boolean;
>      begin
>         writeln (pass_str, 'B');
>         func_b := false;
>      end;
>
>   var
>      // what should this be??
>      my_func : function (pass_str : ansistring) : boolean;
>
>   begin
>      my_func := @func_a;
>      my_func('hello, ');
>   end;
>
> Thanks!
> ~David.
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>



-- 
Lourival J. Mendes Neto



More information about the fpc-pascal mailing list