mzmm56
mzmm56

Reputation: 1292

Cannot find variable `_` in Elm

I'm used to using the es2015 idiom of map(_ => _), but it seems that a single underscore is not a valid variable name for Elm's lambda functions.

Is this correct? If so, is there a resource documenting/explaining this?

The following fails with Cannot find variable `_`

view : Model -> Html Msg
view model =
    div []
        [ ul []
            (List.map (\_ -> li [] [ text _.message ]) model.messages)
        ]

However this works:

            ...

            (List.map (\a -> li [] [ text a.message ]) model.messages)

Upvotes: 1

Views: 820

Answers (1)

Chad Gilbert
Chad Gilbert

Reputation: 36385

The underscore is a reserved character that means you will be ignoring whatever value it represents. It therefore cannot be used as if it were a variable in a function's body.

Upvotes: 6

Related Questions