Crafting Interpreters · Week 7
Crafting Interpreters 12: Classes: reading guide
Read the chapterVerified by tests
Classes: class declarations, instances with fields, property get and set, methods, this bound via a small closure, and initializers with init().
Time About 7 hours this week
Why it matters
Seeing how objects, methods and this are built from closures and maps explains a lot about JavaScript and Python object models.
How to approach it
Implement in order and test after each step. Work through the explanation of how this is bound by an extra environment; draw it.
Challenges
3 challenges at the end of the chapter.
Challenges 1 (static methods) and 2 (getter methods) are both good. Challenge 3 is a design discussion about encapsulation. Do challenges that change Lox's behavior on a separate git branch. The cracked.guide check runs the official test suite for this chapter, and a challenge that changes semantics (new syntax, different errors) can make tests fail on main.
Traps
- Returning the wrong value from init() when called directly.
- Forgetting to bind this when a method is accessed but not immediately called.
Verified by the book's chap12_classes tests when you push your repo.