What does a function with 2 values on the right side mean? (Model -> Html msg)

前端 未结 2 2042
孤独总比滥情好
孤独总比滥情好 2021-02-13 22:40

I have encountered that in the guide:

viewValidation : Model -> Html msg
viewValidation model =
  let
    (color, message) =
      if model.password == model.         


        
2条回答
  •  悲&欢浪女
    2021-02-13 22:42

    Don't mix the type definition with the normal execution of code. Html is not a function, it is a type that takes a parameter to define a type for a view function.

    Html msg is the most general definition you can have as msg is a variable itself, so this returns Html that is independent of the msg type you are currently using. This could either be because it creates no event messages, or because the view function takes messages as parameters.

    As the comments established Html () would be a very narrow type that is constrained to return nothing.

    The most common case though will be a view function returning Html Msg - i.e. Html with messages based on user interactions.

    As Elm encourages componentization, you also need keep Html.map in mind. It's type signature is Html.map : (a -> b) -> Html a -> Html b. In the context of components this is more easily read as

    Html.map : (Child.Msg -> Parent.Msg) -> Html Child.Msg -> Html Parent.Msg
    

    Note that when you define your messages in your parent component, you will have something like:

    type Msg = ChildMsg Child.Msg
    

    which means that ChildMsg has type signature:

    ChildMsg : Child.Msg -> Parent.Msg
    

    So my view functions have a lot of

    parentView model = 
      -- childView model.child |> Html.map ChildMsg
      Html.map ChildMsg (childView model.child)
    

提交回复
热议问题