Thread overview
Cannot instantiate ReturnType with lambda
6 days ago
luafyn
6 days ago
monkyyy
4 days ago
Nick Treleaven
4 days ago
monkyyy
3 days ago
Nick Treleaven
3 days ago
monkyyy
3 days ago
not monkyyy
3 days ago
monkyyy
6 days ago

Why doesn't this work?

import std;

struct Optional(T)
{
    T value;
    bool hasValue;
}

auto map(alias fn, T)(Optional!T m)
{
    alias U = ReturnType!fn;

    if (m.hasValue) {
        return Optional!U(fn(m.value), true);
    } else {
        return Optional!U();
    }
}

void main()
{
    auto x = Optional!string("123", true);
    auto y = x.map!(it => 123);
    writeln(y);
}

Error:

onlineapp.d(11): Error: template instance `onlineapp.main.ReturnType!(__lambda_L23_C21)` does not match template declaration `ReturnType(alias func)`
  with `func = __lambda_L23_C21(__T1)(it)`
  must satisfy the following constraint:
`       isCallable!func`
    alias U = ReturnType!fn;
              ^
onlineapp.d(23): Error: template instance `onlineapp.main.map!((it) => 123, string)` error instantiating
    auto y = x.map!(it => 123);
              ^

This error message makes noooooo sense to me. At all. It's gibberish. How is a lambda not callable?! This is really frustrating, as something that works absolutely trivially in any other language.

It's something that I've ran against with D time and time again: I get an inscrutable error or problem that is only solved by “just” knowing the right incantation to use, anytime I try to do something even slightly unorthodox, and it makes D really annoying to use if you’re new to it.

Please ignore the bad encapsulation in the struct, it's just a minimal example. Thank you

6 days ago

On Saturday, 26 July 2025 at 23:08:35 UTC, luafyn wrote:

>
alias U = ReturnType!fn;

ReturnType!(fn!T) works

id avoid being this explicit with types tho, type inference is unidirectional in d, its best to lean into it and follow the natural flow

4 days ago

On Saturday, 26 July 2025 at 23:08:35 UTC, luafyn wrote:

>

Why doesn't this work?

import std;

struct Optional(T)
{
    T value;
    bool hasValue;
}

auto map(alias fn, T)(Optional!T m)
{
    alias U = ReturnType!fn;

In this case, an untyped function literal (it => 123) is actually a template function (pointer) and there is no way for the compiler to infer what the type of fn should be for ReturnType!fn.

monkyyy's solution is fine, though I'd probably write this:

    alias U = typeof(fn(m.value));
>
if (m.hasValue) {
    return Optional!U(fn(m.value), true);
} else {
    return Optional!U();
}

}

void main()
{
auto x = Optional!string("123", true);
auto y = x.map!(it => 123);
writeln(y);
}


Error:

```d
onlineapp.d(11): Error: template instance `onlineapp.main.ReturnType!(__lambda_L23_C21)` does not match template declaration `ReturnType(alias func)`
  with `func = __lambda_L23_C21(__T1)(it)`
  must satisfy the following constraint:
`       isCallable!func`
    alias U = ReturnType!fn;
              ^
onlineapp.d(23): Error: template instance `onlineapp.main.map!((it) => 123, string)` error instantiating
    auto y = x.map!(it => 123);
              ^

This error message makes noooooo sense to me. At all. It's gibberish. How is a lambda not callable?! This is really frustrating, as something that works absolutely trivially in any other language.

The problem is isCallable (in the general case) cannot detect template functions that are instantiable with IFTI. The docs for it were updated earlier in the month but are currently only on the prerelease version:

https://dlang.org/phobos-prerelease/std_traits.html#isCallable

In Phobos 3 isCallable will likely be replaced. I think when it was made to support some templates with !(), that muddied the waters. It would make sense IMO (possibly with another name) if it only worked with functions, delegates and types with opCall.

Also Steven Schveighoffer has proposed __traits(canCall, expression) which if implemented would be a much better alternative to using isCallable in a template constraint.

>

It's something that I've ran against with D time and time again: I get an inscrutable error or problem that is only solved by “just” knowing the right incantation to use, anytime I try to do something even slightly unorthodox, and it makes D really annoying to use if you’re new to it.

It's interesting to see problems like that, there may be ways to improve the language/compiler error messages/Phobos or the docs to help with these.

4 days ago

On Monday, 28 July 2025 at 11:18:48 UTC, Nick Treleaven wrote:

>

In Phobos 3 isCallable will likely be replaced. I think when it was made to support some templates with !(), that muddied the waters.

Whack-a-moleing; you guys do that allot

The solution I offered at the time while still fundamentally flawed was far more robust then what was merged

3 days ago

On Monday, 28 July 2025 at 18:51:23 UTC, monkyyy wrote:

>

On Monday, 28 July 2025 at 11:18:48 UTC, Nick Treleaven wrote:
The solution I offered at the time while still fundamentally flawed was far more robust then what was merged

What solution?

3 days ago
On Monday, 28 July 2025 at 18:51:23 UTC, monkyyy wrote:
> The solution I offered at the time while still fundamentally flawed was far more robust

Sounds a lot like…

> Whack-a-moleing



3 days ago

On Tuesday, 29 July 2025 at 20:34:27 UTC, Nick Treleaven wrote:

>

On Monday, 28 July 2025 at 18:51:23 UTC, monkyyy wrote:

>

On Monday, 28 July 2025 at 11:18:48 UTC, Nick Treleaven wrote:
The solution I offered at the time while still fundamentally flawed was far more robust then what was merged

What solution?

https://github.com/dlang/phobos/issues/10814#issuecomment-3042904108

3 days ago
On Tuesday, 29 July 2025 at 21:47:05 UTC, not monkyyy wrote:
>
Who are you?