elm-community / html-extra

Additional functions for working with Html.
http://package.elm-lang.org/packages/elm-community/html-extra/latest
MIT License
31 stars 14 forks source link

Add Html.Extra.static #6

Closed jvoigtlaender closed 8 years ago

jvoigtlaender commented 8 years ago

A motivating use case is in this thread on the mailing list.

In short:

One may model the case that one has some html that is static (in the sense it never gives rise to an event) by using type Html Never. Then, when including that piece of html in a view, one will typically have to transform it into something of type Html Msg for an application specific Msg type. One way to do so is to use Html.App.map with a function of type Never -> Msg. Typically \_ -> NoOp. But sometimes this may mean one has to artificially introduce NoOp into the application specific Msg type, which may otherwise not have a need for a NoOp concept. Also, it's lame to write down that \_ -> NoOp function when it is anyway never going to be actually applied. And moreover, writing Html.App.map (\_ -> NoOp) does not actually enforce that the input is of type Html Never, it would accept any other Html Whatever as well.

The function static : Html Never -> Html msg here remedies all that.

For example, we then have that static (Html.text "abcdef") has type Html Msg for whatever Msg type we want, and at the same time we have expressed that the html being embedded there is not allowed to ever actually emit any messages (even if one later refactors and replaces Html.text "abcdef" by another expression or by an argument of the view function, for example).