gleam icon indicating copy to clipboard operation
gleam copied to clipboard

Track previously bound names to improve compiler error messages

Open chouzar opened this issue 2 years ago • 1 comments

In gleam we're capable of rebinding names to new values or functions, this can be very useful when aliasing a previously defined function or applying a partially applied function:

fn add(x, y) { x + y }

// aliasing a previously defined function
pub fn do_add() {
  let operation = add
  operation(10, 10)
}

// partially applying a function
pub fn apply_add() {
  let add = add(10, _) 
  add(20)
}

However once this name is bound the previous expression does no longer exist:

pub fn add(x, y) { x + y }

pub fn say_hello() {
  let add = add(10, 10)  // rebind the add function to an Int value
  add + add(20, 30)
}
error: Type mismatch
   ┌─ ./src/test.gleam:16:9
   │
 5 │   add + add(20, 30)
   │         ^^^

This value is being called as a function but its type is:

    Int

The message above could probably be improved upon by somehow tracking previously bound variables:

error: Type mismatch
   ┌─ ./src/test.gleam:16:9
   │
 5 │   add + add(20, 30)
   │         ^^^

This value is being called as a function but its type is:

    Int

Maybe you mean "add" which was bound at line 4: 

  ┌─ ./src/test.gleam:5:9
  │
4 │   let add = add(10, 10)
  │       ^^^

chouzar avatar Jun 14 '22 05:06 chouzar

Thank you! This would be a really nice improvement.

It's not super clear to me how we would implement this presently. Let's make sure we have a design before work starts.

lpil avatar Jun 16 '22 08:06 lpil