Lambda returning a lambda?

Simen Kjærås simen.kjaras at gmail.com
Thu Feb 1 14:28:34 UTC 2018


On Thursday, 1 February 2018 at 11:51:11 UTC, aliak wrote:
> Is there a way to do this:
>
> import std.stdio;
>
> void main()
> {
>     alias f = (a) => (b) => a * b;
>     f(2)(3).writeln;
> }

The problem here is the outer lambda doesn't know what type the 
inner lambda is. We can rewrite this a bit:

auto f(T)(T a) {
     return b => a * b;
}

What type should this function return? Sure, it's possible to 
define such a type:

auto f(T)(T a) {
     struct Result {
         auto opCall(T2)(T2 b) {
             return a * b;
         }
     }
     Result r;
     return r;
}

However, that type is not a lambda, but a specialized type that 
only does the right thing in some very special cases.

--
   Simen


More information about the Digitalmars-d-learn mailing list