# Enums, several different ways

**URL:** <https://discourse.elm-lang.org/t/enums-several-different-ways/4585>\
**Category:** Request Feedback\
**Created:** [October 30, 2019, 4:02pm UTC](https://discourse.elm-lang.org/t/enums-several-different-ways/4585 "2019-10-30T16:02:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [October 30, 2019, 4:02pm UTC](https://discourse.elm-lang.org/t/enums-several-different-ways/4585/1 "2019-10-30T16:02:15Z")

</div>

With reference to the discussion we had here:

[How to do enums in Elm](https://discourse.elm-lang.org/t/how-to-do-enums-in-elm/1353)

I decided to have a go at creating an enums package for this. There is already one here:

[https://package.elm-lang.org/packages/genthaler/elm-enum/latest/](https://package.elm-lang.org/packages/genthaler/elm-enum/latest/)

One thing I found odd about that one was the signature for decoder:

```
decodeEnumValue : Enum a -> String -> Decoder a

```

Surely it should be:

```
decodeEnumValue : Enum a -> Decoder a

```

So the revised API I came up with is here, along with some examples of how to use it:

[https://gist.github.com/rupertlssmith/2d24347a94efc68d1276d62363d00bbe](https://gist.github.com/rupertlssmith/2d24347a94efc68d1276d62363d00bbe)

I left out the functions for creating HTML `option` or `select`, since those seem not to be a core concern of the data model.

Any thoughts?

---

<div class="post-metadata">

**Author:** ![Herteby](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/herteby/32/1329_2.png) [@Herteby](https://discourse.elm-lang.org/u/Herteby)\
**Post date:** [October 30, 2019, 4:09pm UTC](https://discourse.elm-lang.org/t/enums-several-different-ways/4585/2 "2019-10-30T16:09:18Z")

</div>

Also my package 😉  
[https://package.elm-lang.org/packages/Herteby/enum/latest/](https://package.elm-lang.org/packages/Herteby/enum/latest/)

```elm
type Fruit
    = Apple
    | Banana
    | Mango

fruit : Enum Fruit
fruit =
    Enum.create
        [ ( "Apple", Apple )
        , ( "Banana", Banana )
        , ( "Mango", Mango )
        ]

```

```elm
type alias Enum a =
    { toString : a -> String
    , fromString : String -> Maybe a
    , encode : a -> Value
    , decoder : Decoder a
    , list : List ( String, a )
    }

```

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [October 30, 2019, 4:16pm UTC](https://discourse.elm-lang.org/t/enums-several-different-ways/4585/3 "2019-10-30T16:16:55Z")

</div>

> [@Herteby](#):
>
> Also my package 😉  
> [enum 1.0.1](https://package.elm-lang.org/packages/Herteby/enum/latest/)

Yes, saw that one too. Didn’t seem to be based on the “How to do enums in Elm” discussion though - but not completely dissimilar.

I think I could get `Enum a -> a -> Int` for free by having the `make` function enumerate the list of values, so would not need a separate model for `EnumInt`.

---

<div class="post-metadata">

**Author:** ![miniBill](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/minibill/32/800_2.png) [@miniBill](https://discourse.elm-lang.org/u/miniBill)\
**Post date:** [October 30, 2019, 5:12pm UTC](https://discourse.elm-lang.org/t/enums-several-different-ways/4585/4 "2019-10-30T17:12:35Z")

</div>

I think that requiring the pattern matcher (rather than listing) is better for the user, as it helps not forgetting stuff.  
[I’ve written about it in relation to elm-codec](https://groundlevelmagic.tk/posts/2019-10-24-advanced-elm-codecs/)

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [October 30, 2019, 9:59pm UTC](https://discourse.elm-lang.org/t/enums-several-different-ways/4585/5 "2019-10-30T21:59:06Z")

</div>

> [@miniBill](#):
>
> I think that requiring the pattern matcher (rather than listing) is better for the user

What do you mean by ‘requiring the pattern matcher’? Can you give a little example in code, I don’t quite follow you.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [November 9, 2019, 9:59pm UTC](https://discourse.elm-lang.org/t/enums-several-different-ways/4585/6 "2019-11-09T21:59:09Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
