Created
June 7, 2016 19:11
-
-
Save JoshCheek/20fec71922c0675eb58506b3c5b8a31c to your computer and use it in GitHub Desktop.
An example of how to have a constructor and type with the same name in Elm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- An example of how to have a constructor and type with the same name | |
-- https://github.com/elm-lang/core/blob/3833b53f57f21df7ce7d3cf6664c61ee5671df4b/src/Task.elm#L43-L52 | |
import Html exposing (Html, button, div, text) | |
import Html.App as Html | |
import Html.Attributes exposing (style) | |
import Html.Events exposing (onClick) | |
main = | |
Html.beginnerProgram { model = initialCount, view = view, update = update } | |
-- MODEL | |
type Model = Model String Int | |
initialCount : Model | |
initialCount = | |
Model "Initial value of" 0 | |
-- UPDATE | |
type Msg = Increment | Decrement | |
update : Msg -> Model -> Model | |
update msg (Model desc count) = | |
case msg of | |
Increment -> Model "Incremented to" (count + 1) | |
Decrement -> Model "Decremented to" (count - 1) | |
-- VIEW | |
inline = ("display", "inline") | |
spaceLeft = ("margin", "0.5em") | |
largeText = ("font-size", "1.5em") | |
view : Model -> Html Msg | |
view (Model desc count) = | |
div [style [("margin", "1em")]] | |
[ div | |
[ style [largeText] ] | |
[ text <| desc ++ " " ++ (toString count) ] | |
, button | |
[ onClick Decrement | |
, style [ inline, largeText ] | |
] | |
[ text "-" ] | |
, button | |
[ onClick Increment | |
, style [ inline, largeText, spaceLeft ] | |
] | |
[ text "+" ] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment