[Satirical] Breaking News: The inverted result type!
Kapendev
alexandroskapretsos at gmail.com
Sat Jun 27 10:48:34 UTC 2026
On Saturday, 27 June 2026 at 09:53:06 UTC, Richard (Rikki) Andrew
Cattermole wrote:
> Such a type can be represented with just the following struct:
>
> ```d
> struct InvertedResult(ErrorCause, DataType) {
> ErrorCause error;
>
> alias error this;
>
> DataType data;
> }
> ```
>
> Now you can do such things as:
>
> ```d
> InvertedResult!(string, int) result = ...;
>
> if (string error = result) {
> // oh noes failure!
> } else {
> // we MAY have data here!
>
> if (result.data) {
> // got data
> } else {
> // oh noes, another failure case!
> }
> }
> ```
Yeah, this works. I'm not sure (even for my version of this) if
the `alias this` should be the error value or a function like
`isSome` that checks if `error == noterror`.
Maybe it needs a different name. `CouldError`??
More information about the Digitalmars-d
mailing list