Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding class in two steps in interpreter - is it necessary? #1188

Open
campierce opened this issue Dec 28, 2024 · 0 comments
Open

Binding class in two steps in interpreter - is it necessary? #1188

campierce opened this issue Dec 28, 2024 · 0 comments

Comments

@campierce
Copy link

In chapter 12, when we implement VisitClassStmt in the interpreter, the book has us bind the class in two steps - first define the name (whose value is null), and later reassign that name to the LoxClass we create. The claim is: "That two-stage variable binding process allows references to the class inside its own methods."

That "define it first" idea makes sense for the resolver, where we resolve the methods inline (so the class name must exist upfront). But in the interpreter, we don't evaluate the methods between the two steps, so it should be safe to omit the first step and just bind the class one time, after we've created the corresponding LoxClass. I tried that, and my test cases still work. For example:

class foo {
  bar() {
    print foo;
  }
}

foo().bar(); // prints "foo class"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant