error-message-catalog icon indicating copy to clipboard operation
error-message-catalog copied to clipboard

Minor improvement to case type errors

Open boxed opened this issue 8 years ago • 0 comments

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.

boxed avatar Oct 20 '17 19:10 boxed