Enum is an implementation of OO style Enums in Elm.
The implementation was guided initially by this Discourse discussion, then by usage in my personal projects.
Enum constructor.
type DataType
= Text
| Date
| Email
| Address
| Postcode
| State
| Gender
dataTypeEnum : Enum DataType
dataTypeEnum =
Enum.makeEnum
[ Text
, Date
, Email
, Address
, Postcode
, State
, Gender
]
Basics.toStringReturn the string representation of the Enum.
type DataType
= Text
enum = makeEnum [Text] Basics.toString
(Enum.toString enum Text) == "Text"If possible, return the union type tag from the String representation.
type DataType
= Text
enum = makeEnum [Text] Basics.toString
(Enum.findEnumValue enum "Text") == Ok Text
(Enum.findEnumValue enum "Foo") == Err "Foo"Converts the output of findEnumValue to a Json.Decode.Decoder
Creates an input handler for the Enum.
Takes an Enum, a Msg that tags the Enum's underlying union type, and a currently selected value,
and constructs a <select> element.
view model =
div [] [ Enum.enumSelect Model.dataTypeEnum SetDataType model.dataType, text model.message ]Take a look at the example project for a simple use-case.
Union type representing an
Enum. Note that this an opaque type; usemakeEnumto constructEnums.