On Tuesday, 1 September 2020 at 02:48:08 UTC, Ben Jones wrote:

Thanks all.

I tried using alias this at first and then I get errors trying to construct AliasType objects:


auto pi = Payload!int(5);
auto pe = ParseError("error");
alias PRType = ParseResult!(Payload!int, ParseError);
auto pr = PRType(pi);

gives:

cannot implicitly convert expression `pi` of type `Payload!int` to `SumType!(Payload!int, ParseError)`

Unfortunately, `alias this` does not apply to constructors. You can either add your own constructor to the wrapper struct:

struct ParseResult(Ts...)
{
    SumType!Ts data;
    alias data this;

    this(T)(T t)
        if (staticIndexOf!(T, data.Types) >= 0)
    {
        data = t;
    }
}

...or use a factory function like the following:

PRType parseResult(T)(T t)
{
    return PRType(SumType!(Payload!int, ParseError)(t));
}

Reply via email to