Eli Rose
Eli Rose

Reputation: 7048

Why is it idiomatic to write `Html msg` instead of `Html a`?

I'm a beginner to Elm. This is a question about style, not how something works.

For example, in http://elm-lang.org/examples/form we have

viewValidation : Model -> Html msg

I know this is done so that viewValidation model can coexist in the same list as values of type Html Msg. But, since msg here is just a type variable, why not use the more traditional a?

What extra information is msg intended to convey?

Upvotes: 1

Views: 153

Answers (1)

Chad Gilbert
Chad Gilbert

Reputation: 36385

The Elm Architecture encourages consistent naming of types like Model and Msg to convey their meaning. The use of lowercase msg just makes it a little more clear what the intention is.

It very well could have been named a, but that arguably doesn't convey as much information.

Edit

To elaborate a bit more, consider the documentation for the elm-lang/html package.

Let's take a look at the definition of Html.program:

program :
    { init : (model, Cmd msg)
    , update : msg -> model -> (model, Cmd msg)
    , subscriptions : model -> Sub msg
    , view : model -> Html msg
    } -> Program Never model msg

The Elm Architecture outlines very clear intentions of how your Model and Msg types are to be used by the framework to carry state and indicate what actions are to be performed. By using lowercase msg and model consistently through the documentation, it becomes much easier to understand what goes where.

Consider the alternative of a more Haskellesque approach where the type parameters are often abbreviated starting at a:

program :
    { init : (a, Cmd b)
    , update : b -> a -> (a, Cmd b)
    , subscriptions : a -> Sub b
    , view : a -> Html b
    } -> Program Never a b

That definition is every bit as valid, but I would argue that it is not nearly as explanatory as the documentation for program, which offers strong hints about where your Msg and Model types are to go.

Upvotes: 2

Related Questions