榆树在视图列表中不匹配

时间:2017-07-16 02:27:37

标签: types elm

type Msg
    = NoOp
    | RequestDate
    | ReceiveDate Date
    | UpdateYouTubeUrl YouTubeUrl

-- -

root : Maybe YouTubeUrl -> Html Msg
root youTubeUrl =
    case youTubeUrl of
        Just youTubeUrl ->
            div []
                [ player youTubeUrl
                , urlInput
                ]

        Nothing ->
            urlInput


player : YouTubeUrl -> Html Msg
player youTubeUrl =
    h1 [] [ text ("YouTube player for " ++ youTubeUrl ++ " goes here") ]


urlInput : Html (YouTubeUrl -> Msg)
urlInput =
    input
        [ placeholder "(Enter a YouTube embed URL and hit <Enter>)"
        , onSubmit UpdateYouTubeUrl
        ]
        []

这给我一个类型不匹配错误:

-- TYPE MISMATCH ---------------------------------- ./src/YouTubePlayer/View.elm

The 1st and 2nd entries in this list are different types of values.

28|                 [ player youTubeUrl
29|>                , urlInput
30|                 ]

The 1st entry has this type:

    Html (Msg)

But the 2nd is:

    Html (YouTubeUrl -> Msg)

Hint: It looks like a function needs 1 more argument.

Hint: Every entry in a list needs to be the same type of value. This way you
never run into unexpected values partway through. To mix different types in a
single list, create a "union type" as described in:
<http://guide.elm-lang.org/types/union_types.html>

Detected errors in 1 module. 

错误很明显,问题是我有一个

列表
[ Html Msg
, Html (YouTubeUrl -> Msg)
]

...其中Elm中的列表需要是同质的,但我不明白的是Html (YouTubeUrl -> Msg)urlInput的类型签名的原因。我收集它与我使用onSubmit有关。

我是榆树新手,所以我不确定我做错了什么。 Elm Guide book没有看到Html (a -> Msg)的任何类型示例。

elm 0.18

1 个答案:

答案 0 :(得分:2)

问题源于您使用onSubmit,其签名是:

onSubmit : msg -> Attribute msg

您正在向它传递一个构造函数UpdateYouTubeUrl,它只接受一个参数。要使用Msg制作UpdateYouTubeUrl,您必须传递YouTubeUrl类型的参数。该参数未被传递,因此Elm的编译器告诉您urlElement函数需要YouTubeUrl参数。

我的预感是你打算使用onInput,它接受​​一个字符串值作为输入,你可以通过获取输入的targetValue来使用它。另外,onSubmit通常用于包装输入的表单元素。

在大多数情况下,如果Elm编译器告诉您它正在查找返回值为Html (a -> msg)的视图函数,则可能意味着您错过了函数中的某个参数,因为您的视图应该通常属于Html Msg类型(当然,除了更高级的案例)。