gleam
gleam copied to clipboard
Track previously bound names to improve compiler error messages
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)
│ ^^^
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.