SumType alias can't be deduced?
Paul Backus
snarwin at gmail.com
Mon Feb 21 20:18:46 UTC 2022
On Monday, 21 February 2022 at 18:43:18 UTC, Emmanuelle wrote:
> If you run this, the compiler should emit this error:
>
> ```d
> onlineapp.d(14): Error: template `onlineapp.foobar` cannot
> deduce function from argument types `!()(SumType!(int, Unit))`
> onlineapp.d(8): Candidate is: `foobar(T)(Option!T
> option)`
> ```
>
> If you change `foobar`’s parameter’s type from `Option!T` to
> `SumType!(T, Unit)`, it compiles without error.
This is a long-standing limitation of the D compiler's template
argument deduction: it cannot "see through" `alias` templates to
deduce the underlying type.
The enhancement request to make this work was submitted in 2008:
https://issues.dlang.org/show_bug.cgi?id=1807
Unfortunately, getting the compiler to handle this correctly is
much harder than it looks, and nobody has managed to do it yet,
though several have tried.
As a workaround, you can define your `Optional` type using a
struct instead of an `alias`:
```d
import std.sumtype;
struct Unit {}
struct Option(T) {
SumType!(T, Unit) data;
this(U, this This)(U value)
{
data = value;
}
alias data this;
}
void foobar(T)(Option!T option) {}
void main() {
foobar(Option!int(123));
}
```
More information about the Digitalmars-d-learn
mailing list