[fpc-devel] Delphi anonymous methods

Alexander Shishkin alexvins at mail.ru
Thu Mar 7 20:20:10 CET 2013


03.03.2013 2:22, Sven Barth пишет:
> On 02.03.2013 20:55, Sven Barth wrote:
>>> Also there are open questions which require brainstorm:
>>> 1. Does Pascal needs other implementation of closures which is different
>>> from anonymous methods implementation?
>>
>> I would say no. After all the method implementation itself stays the
>> same, but the captured variables should be different. Or what does this
>> print:
>
> I've now read through your PDF and somehow I'd like to have a better
> implementation in FPC. Let's consider the following code (Note: I've
> never used anonymous methods yet, so this is what I naively would expect):
>
> === code begin ===
>
> var
>    i: LongInt;
>    SomeProc1, SomeProc2: reference to procedure;
> begin
>    i := 42;
>
>    SomeProc1 := procedure
>                 begin
>                   Writeln('Proc1: ' + i);
>                 end;
>
>    i := 21;
>
>    SomeProc2 := procedure
>                 begin
>                   Writeln('Proc2: ' + i);
>                 end;
>
>    SomeProc1;
>    SomeProc2;
> end;
>
> === code end ===
>
> The output I'd expect here is the following:
>
> === output begin ===
>
> 42
> 21
>
> === output end ===
>
> But if I've understood you correctly I'll get the following instead:
>
> === output begin ===
>
> 21
> 21
>

BTW closurs in c++ support both cases (capture by value and by reference).






More information about the fpc-devel mailing list