Skip to content

Instantly share code, notes, and snippets.

@basti1302
Last active October 23, 2015 23:49
Show Gist options
  • Save basti1302/94b1ffde9d1e3414a8e6 to your computer and use it in GitHub Desktop.
Save basti1302/94b1ffde9d1e3414a8e6 to your computer and use it in GitHub Desktop.
Runtime error in Elm due to shadowing

This code produces a runtime error due to shadowing in Elm (Platform version 0.15.1).

The error is Cannot use 'in' operator to search for '_' in undefined. The error happens somewhere in the code for the equals/not-equals comparison in the function given to filter, because one of the objects which are compared for equality (current) is undefined at that time due to the shadowing.

elm/compiler#1018 is probably related. IMHO, this case should even be compile error, not only a warning.

import Html
type alias Model = { a : String }
one = { a = "1" }
two = { a = "2" }
getNext : Model -> Model
getNext current =
let
-- the identifier "current" is shadowed here
(current :: rest) = List.filter (\ p -> p /= current) [ one, two ]
in
List.head rest |> Maybe.withDefault one
main : Html.Html
main =
let model = getNext one
in Html.text model.a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment