[Satirical] Breaking News: The inverted result type!
Richard Andrew Cattermole (Rikki)
richard at cattermole.co.nz
Sat Jun 27 09:53:06 UTC 2026
Are you tired of using the main stream pattern that is the result
type?
Well I have something just for you!
It is the _inverted_ result type.
How it works is quite simple. You provide the error cause, then
provide data that _could_ be available if it isn't present.
The error cause must default to 0 for non-present and greater
than if its present.
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!
}
}
```
Now you may be wondering, do I need a struct for this? Good news
you don't!
```d
DataType v;
if (string error = getIt(v)) {
// on noes a failure!
} else {
if (v) {
// got data
} else {
// on noes, another failure case!
}
}
```
But why does it have to be a string? It doesn't! It can be any
type that evaluates to a truthiness value.
It could be an integer, or even a pointer!
What about if I have a success value that is positive and zero is
a failure? Fix your kernel you pleeb.
But but there are too many failure branches, why can't there be
one???
Go cry about it loser, to merge those three into two requires if
statement support and thats too main stream.
And what do I do if I don't have access to either the data output
or the return value as it is dependent upon a non-blocking
multithreaded option?
Tough luck. Your not getting 5k requests per second. Block like
its a C64.
How could you? Its like writing notTrue for a parameter name! Or
notNotThingButAThing. Get over yourself, if all the code style
books says its bad, its clearly good.
More information about the Digitalmars-d
mailing list