std.sumtype?

Paul Backus snarwin at gmail.com
Fri Mar 26 22:33:16 UTC 2021


On Friday, 26 March 2021 at 02:56:29 UTC, Atila Neves wrote:
> Completely plausible but in my opinion not nearly enough of a 
> reason to offer an escape hatch.

IMO typeIndex is not really an "escape hatch", because while you 
can write:

     if (sumTypeInstance.typeIndex == 2) {
         // ...
     }

...there is still nothing you can do inside the `if` body to 
access the value without going through `match`. To follow the OOP 
analogy, it would be like having access to `instanceof` but not 
downcasts--you can check what the runtime type is, but you still 
need to go through the normal runtime dispatch mechanism to do 
anything with the object.

I think this principle--that you can't touch the SumType's value 
without using `match`--is a good place to draw the line on what 
we're willing to allow in SumType's public API and what we're 
not, but I am happy to hear arguments for other positions. 
Perhaps we can talk about this during the upcoming BeerConf.


More information about the Digitalmars-d mailing list