Holmusk / elm-street

:deciduous_tree: Crossing the road between Haskell and Elm
Mozilla Public License 2.0
88 stars 6 forks source link

Generated decoders for unit type does not compile #74

Closed arbus closed 5 years ago

arbus commented 5 years ago

Given the following Haskell types:

data Foo = Foo ()
    deriving stock (Show, Generic)
    deriving anyclass (Eq, Ord, Hashable)
    deriving (Elm, ToJSON, FromJSON) via ElmStreet Foo

data Bar = Bar () Int
    deriving stock (Show, Generic)
    deriving anyclass (Eq, Ord, Hashable)
    deriving (Elm, ToJSON, FromJSON) via ElmStreet Bar

the following elm code is generated:


-- Types.elm
type Foo
    = Foo ()

type Bar
    = Bar () Int

-- decoder.elm
decodeFoo : Decoder Foo
decodeFoo =
    let decide : String -> Decoder Foo
        decide x = case x of
            "Foo" -> D.field "contents" <| D.map Foo (D.hardcoded ())
            c -> D.fail <| "Foo doesn't have such constructor: " ++ c
    in D.andThen decide (D.field "tag" D.string)

decodeBar : Decoder Bar
decodeBar =
    let decide : String -> Decoder Bar
        decide x = case x of
            "Bar" -> D.field "contents" <| D.map2 Bar (D.index 0 (D.hardcoded ())) (D.index 1 D.int)
            c -> D.fail <| "Bar doesn't have such constructor: " ++ c
    in D.andThen decide (D.field "tag" D.string)

This fails to compile with the following error message:

The 2nd argument to `map` is not what I expect:

145|             "Foo" -> D.field "contents" <| D.map Foo (D.hardcoded ())
                                                           ^^^^^^^^^^^^^^
This `hardcoded` call produces:

    Decoder (() -> b) -> Decoder b

But `map` needs the 2nd argument to be:

    Decoder ()