Primož GabrijelÄiÄ 218 Posted March 7, 2019 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. Ā 1 1 Share this post Link to post
Sherlock 632 Posted March 7, 2019 I see what you did there, @Lars Fosdal š 2 1 Share this post Link to post
Primož GabrijelÄiÄ 218 Posted March 7, 2019 (edited) 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 March 7, 2019 by Primož GabrijelÄiÄ Share this post Link to post
Dalija Prasnikar 1341 Posted March 7, 2019 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) 4 1 Share this post Link to post