Hi everybody,
i’m in the middle of rewriting something into Elm and i run into a weird thing where i get a Type mismatch when calling a function passed as an argument but not if i hardcode the reference(sorry for wrong term maybe) to the same function in the caller instead.
Following code is very simplified to reproduce the problem:
type Model
= Counter1 Counter1Data
| Counter2 Counter2Data
type alias Counter1Data =
{ count : Int
, foo : String
}
type alias Counter2Data =
{ count : Int
, bar : String
}
So my model is a tagged union where both Counter1 and Counter2 have similar but not the same data.
Now i have a view and two ancilliary functions which look like that:
model = Counter1 { count = 0, foo = "" }
view : Model -> Html Msg
view model =
let
updated = updateCounter model
in
case updated of
Counter1 data -> Html.text (String.fromInt data.count)
Counter2 data -> Html.text (String.fromInt data.count)
updateCounter model =
case model of
Counter1 data ->
Counter1 (increment data)
Counter2 data ->
Counter2 (increment data)
increment : { a | count : Int } -> { a | count : Int }
increment a =
{ a | count = a.count + 1 }
So these two ancialliary functions just increment my model and like that it compiles fine and shows the incremented counter on the screen properly.
But when i change the code so that the increment function is not hardcoded in updateCounter but is instead passed to it it crashes with Type Mismatch error:
updateCounter updateFunc model =
case model of
Counter1 data ->
Counter1 (updateFunc data)
Counter2 data ->
Counter2 (updateFunc data)
increment : { a | count : Int } -> { a | count : Int }
increment a =
{ a | count = a.count + 1 }
view : Model -> Html Msg
view model =
let
updated = updateCounter increment model
in
case updated of
Counter1 data -> Html.text (String.fromInt data.count)
Counter2 data -> Html.text (String.fromInt data.count)
And also ti crashes the same way when i’m trying to pass the increment function as lambda:
updateCounter updateFunc model =
case model of
Counter1 data ->
Counter1 (updateFunc data)
Counter2 data ->
Counter2 (updateFunc data)
view : Model -> Html Msg
view model =
let
increment a =
{ a | count = a.count + 1 }
updated = updateCounter increment model
in
case updated of
Counter1 data -> Html.text (String.fromInt data.count)
Counter2 data -> Html.text (String.fromInt data.count)
Also in Ellie here: https://ellie-app.com/qbPNjPpz7FSa1
Second case(crashing): https://ellie-app.com/qbQq7kdCH5ra1
What am i missing here? Why does it work in the first example but not in the second and third?
Thank you!