The future of lambda delegates

nobody nobody at mailinator.com
Wed Aug 16 17:01:18 PDT 2006


Mikola Lysenko wrote:

> Recent D releases (notably 0.161) have updated the syntax and improved the 
> overall usability of lambda delegates. All the syntactic sugar is nice, but 
> it also lays a few traps for the unwary.  Here is a simple example:
> 
> // The Fibonacci numbers are an integer sequence such that
> //        F(n) = F(n - 1) + F(n - 2)
> //        And F(0) = 0, F(1) = 1
> int delegate() fibs()
> {
>     int a = 0;            // Initialize the last two terms of the Fibonacci 
>     int b = 1;
> 
>     return
>     {
>         int c = a + b;     // Calculate the next term in the sequence
>         a = b;               // Shift the previous terms back
>         b = c;
>         return c;            // Return the result
>     };
> }
> 
> This function returns a function which will sequentially evaluate all of the 
> Fibonacci numbers.  Notice that the inner delegate modifies the variables a 
> and b in fibs() scope.  Because of this, it is not guaranteed to work after 
> fibs returns.  This is most irritating, and it greatly restricts the use of 
> this technique. Another potential use for lambda delegates is to create 
> one-line adapter methods.  Consider the following attempt to create a button 
> which will display an arbitrary message when clicked:

It took me awhile to get what you were doing but it is elegant -- or would be if 
it worked. I am sure I am missing something however so I hope you might explain 
why using static storage is not sufficient:

int delegate() fibs()
{
   return
   {
     static int a = 0;
     static int b = 1;
     int c = a + b;
     a = b;
     b = c ;
     return c;
   };
}




More information about the Digitalmars-d mailing list