I am very new and currently trying to learn Elm. I am coming from JS/React and haven't had any previous RFP experience.
I am in the Guide right here: http://guide.elm-lang.org/architecture/user_input/text_fields.html
The part I have problems with is update
and view
:
-- UPDATE
type Msg
= Change String
update : Msg -> Model -> Model
update msg model =
case msg of
Change newContent ->
{ model | content = newContent }
-- VIEW
view : Model -> Html Msg
view model =
div []
[ input [ placeholder "Text to reverse", onInput Change ] []
, div [] [ text (String.reverse model.content) ]
]
Let's start with the Msg declaration. The Guide says:
It takes one argument, in this case the Change function which was created when we declared the Msg type:
Change : String -> Msg
I don't see how this happened here:
type Msg
= Change String
How did we defined a Change function here? How did we define how that function works? To me it looks like we just declared the type of Msg, which somehow contains whatever Change
is and the type String
.
My second question is about the update:
update : Msg -> Model -> Model
update msg model =
case msg of
Change newContent ->
{ model | content = newContent }
To me this looks like update is a higher order function, which takes a Msg
and returns a function Model -> Model
. But then we define a function with two parameters. Does Msg -> Model -> Model
just means all but the last part are parameters?
Then we call the Change
function:
Change newContent ->
{ model | content = newContent }
What I don't get there is the arrow. Usually the arrow comes after a param definition. But here we have the result of a function before the ->
.
I hope my questions make sense, I am just very confused with this (presumably awesome) language.