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

Minor improvement to case type errors #243

Open boxed opened 6 years ago

boxed commented 6 years ago

Whe compiling this:

foo x = 
    case x of
        "a" -> "asd"
        "b" -> 2
        "c" -> 3
        "d" -> 4

The error is:

-- TYPE MISMATCH ------------------------------------------------------ test.elm

The 1st and 2nd branches of this `case` produce different types of values.

 7|     case x of
 8|         "a" -> "asd"
 9|>        "b" -> 2
10|         "c" -> 3
11|         "d" -> 4

The 1st branch has this type:

    String

But the 2nd is:

    number

Hint: All branches in a `case` must have the same type. So no matter which one
we take, we always get back the same type of value.

The compiler should point to the likely culprit as line 8, not line 9. The rule for this would be to check the most common types produced in a case. I bet that in 99% of cases if the compiler just checks 3 cases instead of 2 it could accurately figure out which of the cases was in error.

This is obviously a trivial example, but in more complex code I find that I trigger this type of error by accidentally supplying too few parameters to a function and if I had changed some of the other lines in the case..of it's not directly obvious where the problem is.