Types Elm中的类型推理

Types Elm中的类型推理,types,type-inference,elm,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 ] 错误

我刚开始学习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.

我想你只是缺少了最后的括号

你的代码应该是

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

这是因为
input
函数与
div
一样,需要输入两个列表,一个用于属性,另一个用于它包含的
Html
片段

我想你只是缺少了最后的一些括号

你的代码应该是

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

这是因为
input
函数与
div
一样,需要在input中输入两个列表,一个用于属性,一个用于它包含的另一个
Html
片段!我正在翻文档,但找不到我需要方括号的确切原因。@pietro909刚刚用解释编辑了答案谢谢!很好的解释。需要等待几分钟才能接受您的回答。顺便说一句,ciao@pietro909,sono Marco,ci siamo conosciuti al-CodemotionGrande!新奥尔良市交通局!榆树是惊人的,我应该尝试过它之前:-)确实它的工作,谢谢!我正在翻文档,但找不到我需要方括号的确切原因。@pietro909刚刚用解释编辑了答案谢谢!很好的解释。需要等待几分钟才能接受您的回答。顺便说一句,ciao@pietro909,sono Marco,ci siamo conosciuti al-CodemotionGrande!新奥尔良市交通局!Elm太棒了,我以前应该试过:-)