Tested in lox playground, for the test case 3, if the m(instance)
call is commented out:
class Confused {
method() {
fun inner(instance) {
var feeling = "confused";
print this.feeling;
}
return inner;
}
}
var instance = Confused();
var m = instance.method();
// m(instance);
lox will not throw an error and run successfully.
If we add a print before m(instance)
:
class Confused {
method() {
fun inner(instance) {
var feeling = "confused";
print this.feeling;
}
return inner;
}
}
var instance = Confused();
var m = instance.method();
print 123;
m(instance);
lox would parse it successfully and run it as well, and 123
would print in console, not shown in the page.
So it should be a runtime error instead of compilation error. And it’s not a good idea to track binding properties on an object during parse stage of a dynamic language.