[Lazarus] polymophic function pointers

Michael Van Canneyt michael at freepascal.org
Wed Feb 27 09:19:22 CET 2013



On Wed, 27 Feb 2013, xrfang wrote:

> Hi,
> Is it possible to do the following:
> 
> === example ===
> 
> program test;
> type
> 
>   TMyClass = class
>   type
>     TDataFilter = function 
>   public
>     Value : Double;
>     Filter: TDataFilter;
>     procedure ViewData;
>   end;
> procedure TMyClass.ViewData;
> begin
>   if Filter = nil then
>     WriteLn(Value)
>   else
>     WriteLn(Filter(Value));
> end;
> 
> var
>   t : TMyClass;
> begin
>   t := TMyClass.Create;
>   t.Value = 2;
>   t.Filter := @sqr;   //this does not work
>   t.Filter := @ln;    //this does not work either
> &nbs p; t.ViewData;
>   t.Free;
> end.
> =========
> 
> The problem is, while assign function pointers to Filter, I would like to make it accept all numeric functions.  That is, if I
> define it to accept Double, it should accept Single and Integers, and if I define it to accept Extended, it should accept all
> numbers.
> 
> Possible?

No.

Michael.


More information about the Lazarus mailing list