Link to an overloaded function (or procedure)

I use type

type
  TRealFunction = reference to function(const X: extended): extended;

a lot in my code. Suppose I have a variable

var
  rfcn: TRealFunction;

and try to assign it Math.ArcSec:

rfcn := ArcSec;

This works the same as expected in Delphi 2009, but now I tried to compile it in Delphi 10.2 and the compiler is upset:

[dcc32 Error] Unit1.pas(42): E2010 Incompatible types: 'TRealFunction' and 'ArcSec'

The difference seems to be that it is ArcSecoverloaded in Delphi 10.2: it is found in single, doubleand extendedvariants. It seems that the compiler does not like references to overloaded functions (or procedures) of this kind (too similar types?).

However, if I redefine

type
  TRealFunction = function(const X: extended): extended;

it just compiles.

Of course, there are obvious workarounds here: I could identify

function ArcSec(const X: extended): extended; inline;
begin
  result := Math.ArcSec(X);
end;

or I could just write

rfcn := function(const X: extended): extended
  begin
    result := Math.ArcSec(x);
  end;

However, this is a lot of code for writing. Is there an easier way around this?

+6
1

:

type
  TRealFunction = function(const X: extended): extended;
const
  rc : TRealFunction = Math.ArcSec;
type
  TRealFunctionRef = reference to function(const X: Extended) : Extended;

var
  rfcn: TRealFunctionRef;
begin
  rfcn := rc;
  ...

, , , .

+4

All Articles