Sign in Join

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

Mailing lists / lazarus / 2013-02

polymophic function pointers

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

xrfang

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
? 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?

Thanks,
Shannon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130227/8896a1b4/attachment.html>

Reply by email

Michael Van Canneyt
to xrfang
xrfang wrote 41 lines
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.

Reply by email