Built-in sumtypes, case and noreturn: why block enders should be expressions.

FeepingCreature feepingcreature at gmail.com
Tue Oct 25 05:24:33 UTC 2022


On Monday, 24 October 2022 at 17:45:17 UTC, rikki cattermole 
wrote:
> On 24/10/2022 7:20 PM, FeepingCreature wrote:
>> 
>> ```
>> (int | string) foo;
>> ...
>> int i = foo.case(int i: i, string s: return null);
>> ```
>
> That is kinda brilliant.
>
> The first case sets what actually gets returned the others all 
> return nothing and are inherited from it.
>
> I assume if the other cases get hit that it sets to 0? (unless 
> of course its boxed)

Well, it doesn't set it to anything, that's sort of the point. :) 
The other cases are `bottom`, which is to say they implconv to 
`int`, but only under the assurance that it'll never come up 
because any control flow that leaves `return null` is unreachable 
anyways. If anything, I'd put `assert(false)` in there, just to 
be safe.

Also, the order doesn't actually matter. `foo.case(string s: 
return null, int i: i)` would also work, because `bottom | int = 
int | bottom = int`.


More information about the Digitalmars-d mailing list