在Elm中输入推断

时间:2016-06-03 07:57:44

标签: types type-inference elm

我刚刚开始学习Elm而且我遇到了类型注释问题。

此代码有效:

viewInput : String -> Html msg
viewInput myText =
    div [ style [("color","red")] ] [ text myText ]

这会引发编译器异常:

viewInput : String -> Html msg
viewInput myText =
    input [ type' "text", placeholder myText ]

错误是

-- TYPE MISMATCH ------------------------------------------------------ form.elm

The type annotation for `viewInput` does not match its definition.

62| viewInput : String -> Html msg
                ^^^^^^^^^^^^^^^^^^
The type annotation is saying:

    String -> Html a

But I am inferring that the definition has this type:

    String -> List (Html a) -> Html a

Detected errors in 1 module.

1 个答案:

答案 0 :(得分:2)

我认为你最后只是遗漏了一些括号......

您的代码应为

viewInput : String -> Html msg
viewInput myText =
    input [ type' "text", placeholder myText ] []

这是因为input函数,div确实需要输入两个列表,一个用于属性,一个用于包含的其他Html