www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [Satirical] Breaking News: The inverted result type!

reply Richard (Rikki) Andrew Cattermole <richard cattermole.co.nz> writes:
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.
Jun 27
next sibling parent monkyyy <crazymonkyyy gmail.com> writes:
On Saturday, 27 June 2026 at 09:53:06 UTC, Richard (Rikki) Andrew 
Cattermole wrote:
 ```d
 struct InvertedResult(ErrorCause, DataType) {
     ErrorCause error;

     alias error this;

     DataType data;
 }
 ```
breaks ufcs You may think that 7 lines of nested if statements is the height of usability, **but NO** ```d struct GooderResult(ErrorCause, DataType){ ErrorCause error; DataType data; alias data this; } auto gooderresult(A,B)(A a,B b)=>GooderResult!(A,B)(a,b); ``` now users can just ignore the error entirely, without truthyness or an opinion on the subject, or inline handleing; and as we all know from the important thread of the philosophy of software design and walter, the best error is one that is defined to not exist Therefore d style should evolve towards this: ```d auto safedevide(int a,int b)=>gooderresult(b?"":"/0",b?a/b:int.max); ``` and for the rare cases you want to fail(why fail when you could just succeed? masochist) ```d auto CRASH(T)(T t){ assert(t.error,t.error.to!string); return t.data; } ``` Or more realistically: ```d auto replacefailure(T,S)(T t,S replace)=>t.error?t.data:replace; ```
Jun 27
prev sibling next sibling parent reply Kapendev <alexandroskapretsos gmail.com> writes:
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`??
Jun 27
parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Saturday, 27 June 2026 at 10:48:34 UTC, Kapendev wrote:
 
 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`??
alias to the data, use opCast bool before even consider designing around truthyness language level truthyness so bad
Jun 27
parent reply Kapendev <alexandroskapretsos gmail.com> writes:
On Saturday, 27 June 2026 at 10:53:42 UTC, monkyyy wrote:
 On Saturday, 27 June 2026 at 10:48:34 UTC, Kapendev wrote:
 
 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`??
alias to the data, use opCast bool before even consider designing around truthyness language level truthyness so bad
I don't like using opCast :)
Jun 27
parent reply Dom Disc <dominikus scherkl.de> writes:
On Saturday, 27 June 2026 at 11:12:02 UTC, Kapendev wrote:
 On Saturday, 27 June 2026 at 10:53:42 UTC, monkyyy wrote:
 [...] use opCast bool before even consider designing around 
 truthyness
I don't like using opCast :)
Instead of ```cast(bool)x``` you can use the much more writeable (but unfortunately not as readable) ```!!x```
Jun 29
parent "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
On 30/06/2026 8:20 AM, Dom Disc wrote:
 On Saturday, 27 June 2026 at 11:12:02 UTC, Kapendev wrote:
 On Saturday, 27 June 2026 at 10:53:42 UTC, monkyyy wrote:
 [...] use opCast bool before even consider designing around truthyness
I don't like using opCast :)
Instead of ```cast(bool)x``` you can use the much more writeable (but unfortunately not as readable) ```!!x```
That is ok, on this thread we care about having neither.
Jun 29
prev sibling parent Guillaume Piolat <first.name gmail.com> writes:
On Saturday, 27 June 2026 at 09:53:06 UTC, Richard (Rikki) Andrew 
Cattermole wrote:
 How could you? Its like writing notTrue for a parameter name!
What if... things were going according to the plan? Nice way to invert the premice in "error-first" programs.
Jun 27