draft proposal for Sum Types for D

Paul Backus snarwin at gmail.com
Mon Dec 5 14:50:47 UTC 2022


On Monday, 5 December 2022 at 14:17:47 UTC, Jacob Shtokolov wrote:
> Another essential and already mentioned feature is 
> extensibility: imagine we have a typed event bus of the 
> following configuration:
>
> ```d
> alias Events = SumType!(Connected, Disconnected, TextMessage);
> ```
>
> Now we want to extend it with new types of events without 
> losing support in the parts of the code we don't control (e.g. 
> library code, etc.). How would we do that?
>
> [...]
>
> Right now it's only possible by using classes, or by 
> introducing a struct with discriminator/tag and a union field 
> manually checking (and casting) the types at runtime.

IMO if you want this kind of extensibility, a sum type is the 
wrong tool for the job. This is *exactly* the use case that 
classes are designed for: extending existing code to operate on 
new types, without modifying the code itself.

Sum types, by contrast, excel when the set of types is fixed, but 
you would like to be able to define new operations on that set of 
types without changing existing code.


More information about the Digitalmars-d mailing list