Trouble compiling msg with type parameter

Greetings!
I have trouble compiling the following code. It’s available on Ellie: https://ellie-app.com/7SS2VZ8b24Pa1

module Main exposing (main)

import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)


type alias Model =
    { count : Int }


initialModel : Model
initialModel =
    { count = 0 }


type Msg i
    = Increment {i | count : Int}
    | Decrement


update : Msg i -> Model -> Model
update msg model =
    case msg of
        Increment m ->
            { model | count = m.count + 1 }

        Decrement ->
            { model | count = model.count - 1 }


view : Model -> Html (Msg Model)
view model =
    div []
        [ button [ onClick<| Increment model] [ text "+1" ]
        , div [] [ text <| String.fromInt model.count ]
        , button [ onClick Decrement ] [ text "-1" ]
        ]


main : Program () Model (Msg Model)
main =
    Browser.sandbox
        { init = initialModel
        , view = view
        , update = update
        }

I get the following error:

    Something is off with the body of the `view` definition:

34|>    div []
35|>        [ button [ onClick<| Increment model] [ text "+1" ]
36|>        , div [] [ text <| String.fromInt model.count ]
37|>        , button [ onClick Decrement ] [ text "-1" ]
38|>        ]

This `div` call produces:

    Html (Msg {})

But the type annotation on `view` says it should be:

    Html (Msg Model)

Hint: Looks like the count field is missing.

Can anyone help me figure this out? Thanks!

Change Msg Model to Msg {} in view and main and it compiles: https://ellie-app.com/7SSNwv8LhKVa1

When you add a type annotation for something that uses an extensible record, you need to specify all other fields:

type alias Person a =
    { a | name : String }


john : Person { age : Int }
john =
    { name = "John", age = 40 }
1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.