Jump to content

Recommended Posts

Yes, they work šŸ™‚Ā (and why wouldn't they?).Ā 

Ā 

program RecursiveAnon;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

function MakeFib: TFunc<integer,int64>;
var
  fib: TFunc<integer,int64>;
begin
  fibĀ :=
    function (value: integer): int64
    begin
      if value < 3 then
        ResultĀ := 1
      else
        ResultĀ := fib(value - 1) + fib(value -2);
    end;
  ResultĀ := fib;
end;

begin
  var fibĀ := MakeFib;
  for var iĀ := 1 to 10 do
    Write(fib(i), ' ');

  Readln;
end.

Ā 

  • Like 1
  • Thanks 1

Share this post


Link to post
10 minutes ago, Sherlock said:

But is it still anonymous though?

Ā 

Of course it is! You can do this:

for iĀ := 1 to 10 do
  Writeln(MakeFib()(i), ' ');

Ā 

As Stefan noted on a side-channel, calling MakeFib causes a memory leak because the anonfunc interface now contains a cyclic reference to itself.

Edited by Primož Gabrijelčič

Share this post


Link to post
21 minutes ago, Primož Gabrijelčič said:

As Stefan noted on a side-channel, calling MakeFib causes a memory leak because the anonfunc interface now contains a cyclic reference to itself.

var
  [weak] fib: TFunc<integer,int64>;

Marking it as weak breaks the cycle. However, there was a bug with weakĀ that is only recently fixed (not sure whether in 10.3 or 10.3.1)

  • Like 4
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Ɨ