effekt
effekt copied to clipboard
Unhelpful error message on missing braces in an interface implementation
When trying to write an instance of an interface:
interface Eff {
def op(): Int
}
def e: Eff = new Eff {
def op() = println("42"); 42
}
shows an error: "Operation Eff is not part of interface Eff."
Here's a screenshot:
One way to work around this is to add braces into the method body:
interface Eff {
def op(): Int
}
def e: Eff = new Eff {
def op() = { println("42"); 42 }
}
TBH it took me quite a while to understand this error...
The parser parses it as
def e: Eff = new Eff { () =>
def op() = println("42")
42
}
which is our syntax for singleton operations and thus shorthand for:
def e: Eff = new Eff {
def Eff() = {
def op() = println("42")
42
}
}
@jiribenes what would you expect as an error message? We could also forbid parsing it as single operation in case it starts with def
I wouldn't know an elegant way to prevent it though.
https://github.com/effekt-lang/effekt/blob/49f2166f85c228b6169d1e7d0c1f508081c5551a/effekt/shared/src/main/scala/effekt/Parser.scala#L426-L437
The parser for implementation reuses functionArg
which admits the program you wrote.
After #495, the error looks something like this:
which means I can somewhat improve it (or at least underline the relevant part) by consuming tokens after ;
until I find }
or def
or EOF: