Thread overview
cannot deduce template lambda from argument
Dec 05, 2017
aliak
Dec 06, 2017
Biotronic
Dec 06, 2017
aliak
Dec 06, 2017
Jonathan M Davis
Dec 06, 2017
aliak
December 05, 2017
Hi,

Having a little trouble understanding lambda type deduction. I have this lambda:

immutable lambda(T) = (T n) => n * n;

and if I call it with an explicit type it works else it errors with: lambda cannot deduce function from argument types !()(int)

auto x = lambda!int(2); // ok
auto x = lambda(2); // errors

But if it's a normal template function then calling it without explicit type is ok.

Thanks for any help!

December 06, 2017
On Tuesday, 5 December 2017 at 23:01:43 UTC, aliak wrote:
> immutable lambda(T) = (T n) => n * n;

Generally, you'd want to write

    alias lambda = n => n * n;

instead. That said, I don't see any reason why your syntax shouldn't work, and would argue it's a bug. Please file it in Bugzilla.

--
  Biotronic
December 06, 2017
On Wednesday, 6 December 2017 at 08:10:26 UTC, Biotronic wrote:
> On Tuesday, 5 December 2017 at 23:01:43 UTC, aliak wrote:
>> immutable lambda(T) = (T n) => n * n;
>
> Generally, you'd want to write
>
>     alias lambda = n => n * n;
>
> instead. That said, I don't see any reason why your syntax shouldn't work, and would argue it's a bug. Please file it in Bugzilla.
>
> --
>   Biotronic

Ok thanks! Done: https://issues.dlang.org/show_bug.cgi?id=18037

Btw, in the case of your suggested approach, what if I want to constrain the parameter type?
December 06, 2017
On Wednesday, December 06, 2017 10:43:18 aliak via Digitalmars-d-learn wrote:
> On Wednesday, 6 December 2017 at 08:10:26 UTC, Biotronic wrote:
> > On Tuesday, 5 December 2017 at 23:01:43 UTC, aliak wrote:
> >> immutable lambda(T) = (T n) => n * n;
> >
> > Generally, you'd want to write
> >
> >     alias lambda = n => n * n;
> >
> > instead. That said, I don't see any reason why your syntax shouldn't work, and would argue it's a bug. Please file it in Bugzilla.
> >
> > --
> >
> >   Biotronic
>
> Ok thanks! Done: https://issues.dlang.org/show_bug.cgi?id=18037
>
> Btw, in the case of your suggested approach, what if I want to constrain the parameter type?

If you only want one type, then given n that type; I'm pretty sure that it would be

alias lambda = (int n) => n * n;

if you wanted an int. But if you want to do anything more complicated with it, it would make more sense to just turn it into a proper function template.

- Jonathan M Davis

December 06, 2017
On Wednesday, 6 December 2017 at 11:02:01 UTC, Jonathan M Davis wrote:
> If you only want one type, then given n that type; I'm pretty sure that it would be
>
> alias lambda = (int n) => n * n;
>
> if you wanted an int. But if you want to do anything more complicated with it, it would make more sense to just turn it into a proper function template.
>
> - Jonathan M Davis

Roight, I was more thinking along the lines of a little more complicated I guess :)

i.e.:

template lambda(T)
    if (isIntegral!T)
{
    alias lambda = (T n) => n * n;
}

But you're right, if it gets more complicated a proper function is probably better. Plus I just tried and it seems like you can't really constrain a variable template anyway. Unless there's some magic syntax I've overlooked in the docs.

Thanks!

Thanks!