elm / error-message-catalog

A catalog of broken Elm programs / data to improve error messages
BSD 3-Clause "New" or "Revised" License
173 stars 17 forks source link

Functions inside case statements #253

Closed joshhornby closed 6 years ago

joshhornby commented 6 years ago

If you try and use a function inside a case statement the results are potentially unexpected

decoder : Decode.Decoder String
decoder =
    Decode.at [ "example", "foo" ] Decode.string
        |> Decode.andThen
            (\string ->
                case string of
                    "test" ->
                        Decode.succeed "Working"

                    wrappedInAFunc ->
                        Decode.succeed "Working"

                    _ ->
                        Decode.fail <| "Invalid " ++ string
            )

wrappedInAFunc : String
wrappedInAFunc =
    "test"

Will throw:

The following pattern is redundant. Remove it.Any value with this shape will be handled by a previous pattern.

This is because you can't call a function as you need to use a 'pattern'.

Maybe the error message can check if a function is being used, and if so warn the user about what they are doing as well as telling them the pattern is redundant?

Example Ellie: https://ellie-app.com/jBM3qQDFMa1/0

evancz commented 6 years ago

It's redundant because pattern matching does not work like in Erlang or Elixir.

In the next release of the compiler, this will be caught because the compiler disallows variable name shadowing. I think that will also be helpful for folks coming from Erlang and Elixir who are expecting pattern matches to work different.

I recommend asking around on the slack channel for further clarification in the meantime. And thanks for the report!