Idea: Extensible Union types and benefits they bring for real world Elm code

I like this idea. I think it only works though when being used to construct members of an ‘extensible’ union type, rather than to pattern match on them as inputs. In the expression below, the msg is always the return type of some function passed in, not the input type. Is the right way to describe this to say that this msg type is always in the contravariant position? Too long since I studied the theory…

{ a
| goToPage : Url -> msg
, toggleDarkTheme : msg
}

But I think you are on the right track as far as I am concerned. You can sort of do extensibility or sub-sets of union types in Elm in a number of ways. It may add some complexity, but I also think its an attractive property of Elm that it forces you to trade complexity for flexibility - do you really need the flexibility? if so, you must pay a price for it.

Also worth noting @gampleman’s proposed encoding in Elm too:

https://discourse.elm-lang.org/t/how-to-represent-many-subsets-of-an-enum/6088/18

2 Likes