Search code examples
haskellelm

What is the Elm equivalent of Haskell's Show?


I implemented my custom type in Elm:

type Bin = BEmpty | One Bin | Zero Bin

I want it to be showable when I use it in String context similar to:

main = 
  Html.p (text bin)

if, for example, following is defined:

bin : Bin
bin = One (One (Zero BEmpty))

If I try to define instance of Show as follows:

instance Show Bin
  show bin = show (binToDec bin)

I get following error:

-- NAMING ERROR --------------------------------------------------- src/Main.elm

I cannot find a `Bin` variant:

19| instance Show Bin 
                  ^^^
These names seem close though:

    EQ
    Err
    GT
    L

Is there an equivalent of Haskell's instance Show Bin where declaration in Elm?


Solution

  • No, Elm does not have this feature. You need to implement your own toString function:

    binToString : Bin -> String
    binToString bin =
        binToDec bin
        |> String.fromInt