Re: Return values from auto function

2020-11-07 Thread Piotr Mitana via Digitalmars-d-learn
On Friday, 6 November 2020 at 10:51:20 UTC, Andrey Zherikov wrote: struct Result(T) { struct Success { static if(!is(T == void)) { T value; } } struct Failure { string error; } Algebraic!(Success, Failure) result; this

Re: Return values from auto function

2020-11-07 Thread Jesse Phillips via Digitalmars-d-learn
On Saturday, 7 November 2020 at 15:49:13 UTC, James Blachly wrote: ``` return i > 0 ? cast(Result) Success!int(i) : cast(Result) Failure("Sorry"); ``` I don't know about the SumType but I would expect you could use a construction instead of cast. import std; alias Result = Algebraic!

Re: Return values from auto function

2020-11-07 Thread Patrick Schluter via Digitalmars-d-learn
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 kno

Re: Return values from auto function

2020-11-07 Thread James Blachly via Digitalmars-d-learn
On 11/6/20 5:51 AM, Andrey Zherikov wrote: I have auto function 'f' that might return either an error (with some text) or a result (with some value). The problem is that the type of the error is not the same as the type of result so compilation fails. ... How can I make the original code comp

Re: Return values from auto function

2020-11-07 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 7 November 2020 at 01:50:15 UTC, Jesse Phillips wrote: On Friday, 6 November 2020 at 15:06:18 UTC, Andrey Zherikov wrote: On Friday, 6 November 2020 at 14:58:40 UTC, Jesse Phillips wrote: On Friday, 6 November 2020 at 14:20:40 UTC, Andrey Zherikov wrote: This issue seems hit the in

Re: Return values from auto function

2020-11-06 Thread Jesse Phillips via Digitalmars-d-learn
On Friday, 6 November 2020 at 15:06:18 UTC, Andrey Zherikov wrote: On Friday, 6 November 2020 at 14:58:40 UTC, Jesse Phillips wrote: On Friday, 6 November 2020 at 14:20:40 UTC, Andrey Zherikov wrote: This issue seems hit the inability to implicitly convert custom types. May be it makes more sen

Re: Return values from auto function

2020-11-06 Thread Jesse Phillips via Digitalmars-d-learn
On Friday, 6 November 2020 at 20:05:36 UTC, Ferhat Kurtulmuş wrote: On Friday, 6 November 2020 at 10:51:20 UTC, Andrey Zherikov wrote: I have auto function 'f' that might return either an error (with some text) or a result (with some value). The problem is that the type of the error is not the

Re: Return values from auto function

2020-11-06 Thread Ferhat Kurtulmuş via Digitalmars-d-learn
On Friday, 6 November 2020 at 10:51:20 UTC, Andrey Zherikov wrote: I have auto function 'f' that might return either an error (with some text) or a result (with some value). The problem is that the type of the error is not the same as the type of result so compilation fails. [...] Sounds li

Re: Return values from auto function

2020-11-06 Thread Mike Parker via Digitalmars-d-learn
On Friday, 6 November 2020 at 15:06:18 UTC, Andrey Zherikov wrote: To clarify my statement: Yes, Result!void and Result!int are different types but I couldn't find a way to implicitly convert one to another. You can't. Structs do not implicitly convert to each other, templated or otherwise.

Re: Return values from auto function

2020-11-06 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 6 November 2020 at 14:58:40 UTC, Jesse Phillips wrote: On Friday, 6 November 2020 at 14:20:40 UTC, Andrey Zherikov wrote: This issue seems hit the inability to implicitly convert custom types. May be it makes more sense to ask in a separate thread. The return type must be the same

Re: Return values from auto function

2020-11-06 Thread Jesse Phillips via Digitalmars-d-learn
On Friday, 6 November 2020 at 14:20:40 UTC, Andrey Zherikov wrote: On Friday, 6 November 2020 at 12:03:01 UTC, Paul Backus wrote: 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 te

Re: Return values from auto function

2020-11-06 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 6 November 2020 at 12:03:01 UTC, Paul Backus wrote: 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. This issue seems hit the inability to implicitly convert c

Re: Return values from auto function

2020-11-06 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 6 November 2020 at 13:59:58 UTC, gbram wrote: On Friday, 6 November 2020 at 12:03:01 UTC, Paul Backus wrote: On Friday, 6 November 2020 at 10:51:20 UTC, Andrey Zherikov wrote: How can I make the original code compilable without templatizing `failure` function? You can't. Both ret

Re: Return values from auto function

2020-11-06 Thread gbram via Digitalmars-d-learn
On Friday, 6 November 2020 at 12:03:01 UTC, Paul Backus wrote: On Friday, 6 November 2020 at 10:51:20 UTC, Andrey Zherikov wrote: 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 fa

Re: Return values from auto function

2020-11-06 Thread Paul Backus via Digitalmars-d-learn
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 ? succe