Return values from auto function
Patrick Schluter
Patrick.Schluter at bbox.fr
Sat Nov 7 16:02:16 UTC 2020
On Saturday, 7 November 2020 at 15:49:13 UTC, James Blachly wrote:
>
> ```
> retval = i > 0 ? Success!int(i) : Failure("Sorry");
> ```
>
> casting each to `Result` compiles, but is verbose:
>
> ```
> return i > 0 ? cast(Result) Success!int(i) : cast(Result)
> Failure("Sorry");
> ```
>
> ** Could someone more knowledgeable than me explain why
> implicit conversion does not happen with the ternary op, but
> works fine with if/else? Presumably, it is because the op
> returns a single type and implicit conversion is performed
> after computing the expression's return type? If this somehow
> worked, it would make the SumType package much more ergonomic **
It's just that tenary requires the same type in both branches. It
was already so in C.
return i > 0 ? (retval = Success!int(i)) : (retval =
Failure("Sorry"));
should work
More information about the Digitalmars-d-learn
mailing list