Return values from auto function
Paul Backus
snarwin at gmail.com
Fri Nov 6 12:03:01 UTC 2020
On Friday, 6 November 2020 at 10:51:20 UTC, Andrey Zherikov wrote:
> I can make it work if I add a type to `failure` function but
> this remove brevity in usage:
> -----------------
> auto failure(T)(string error)
> {
> return Result!T(Result!T.Failure(error));
> }
> auto f(int i)
> {
> return i > 0 ? success(i) : failure!int("err text"); //
> no error
> }
> -----------------
>
> How can I make the original code compilable without
> templatizing `failure` function?
You can't. Both return values have to have the same type, which
means the failure function has to be able to return more than one
type, which means it has to be a template.
More information about the Digitalmars-d-learn
mailing list