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

When argument used with inconsistent types, compiler only points out one use. #230

Open dela3499 opened 7 years ago

dela3499 commented 7 years ago

I used a function argument inconsistently, where one use treated it like a record, and another as a comparable. The first use was correct, and the second was a mistake. The error message pointed to the correct use and didn't mention the incorrect one. It took a while to realize where I'd made the mistake.

I created a function without annotations, like so:

import Dict

f record = 
  let x = record.a
      y = Dict.get record Dict.empty -- accidentally typed "record" rather than "record.a"
  in 0

and misused record. The compiler gave the following message:

image

While the y = definition is where I made the mistake, the compiler doesn't mention it. In this case, it would be great for the compiler to say something like " in y = ..., record is a comparable, but in x = ... it's being treated like a {a: a}.

I get a much better error message after adding a type annotation to the function.

import Dict

f: {a: String} -> Int
f record = 
  let x = record.a
      y = Dict.get record Dict.empty -- accidentally typed "record" rather than "record.a"
  in 0

image