Skip to content

Instantly share code, notes, and snippets.

@tensiondriven
Last active August 14, 2016 22:36
Show Gist options
  • Save tensiondriven/04a36f9deb84b7823296cda1c89d42f4 to your computer and use it in GitHub Desktop.
Save tensiondriven/04a36f9deb84b7823296cda1c89d42f4 to your computer and use it in GitHub Desktop.
Elm complier confusing error?

I have the following model and update function:

type alias Model =
  { people: List String
  , entry: String
  }

model : Model
model =
  { people = ["foo2"]
  , entry = "foo"
  }
  
update : Msg -> Model -> Model
update msg model =
  case msg of
    AddNewParticipant ->
      { model | people = List.append model.people model.entry }

On line 19, we see:

      { model | people = List.append model.people model.entry }

Elm complier gives the error:

-- TYPE MISMATCH ------------------------------------------------------- app.elm

The type annotation for `update` does not match its definition.

41| update : Msg -> Model -> Model
             ^^^^^^^^^^^^^^^^^^^^^
The type annotation is saying:

    Msg -> { ..., entry : String } -> { ..., entry : String }

But I am inferring that the definition has this type:

    Msg -> { ..., entry : List String } -> { ..., entry : List String }

The real problem is that I'm passing a String to the List.append function.

      { model | people = List.append model.people model.entry }

should be

      { model | people = List.append model.people [model.entry] }

So why isn't the compiler telling me that the problem is with List.append? Telling me the problem is with update is confusing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment