cedd
cedd

Reputation: 1861

Elm shorthand to supply type variables to a Union Type

I have an Elm type like this

type ValueAndContext valueType contextType
    = ValueAndContext valueType contextType

I would like to create a new type, with the specific type parameters, to save keyboard taps and screen pixels, maybe like this

type IntWithStringContext = ValueAndContext Int String

I could create a type alias like this, but it requires me to introduce an extra variable to hold the type, so doesn't save that many taps or pixels.

type alias IntWithStringContext =
  { update: ValueAndContext Int String }

I don't think anything else is possible in Elm, but any ideas welcome.

Upvotes: 1

Views: 124

Answers (1)

Chad Gilbert
Chad Gilbert

Reputation: 36375

Sure, you were pretty close with your first example. You can use an alias like this:

type alias IntWithStringContext = ValueAndContext Int String

Perhaps it is more common to see type aliases for record types, but you can also alias union types.

Edit

This will alias the type name but you still have to use the original constructor:

get : IntWithStringContext
get = ValueAndContext 1 "hello"

Upvotes: 5

Related Questions