Crafting Interpreters · Week 10
Ch 12: Classes
Updated
Summary
Crafting Interpreters chapter 12, Classes: class declarations, instances with fields, property get and set, methods, this bound via a small closure, and initializers with init(). Budget about 7 hours; the book's chap12_classes tests check your code.
This week's steps
4 stepsImplement in order and test after each step.
Write the code in your lox repo
Type it into jlox/ (Java) yourself as you read, and get it running.
Challenges 1 (static methods) and 2 (getter methods) are both good.
Push and let the tests verify it
Your repo runs the book's chap12_classes tests on every push; passing marks this done.
Sign in to track this: tick each step off and it counts toward your week.
Try it in the browser
Optionaljlox as it stands at the end of chapter 12: what this chapter adds works, and anything from a later chapter is the error your own jlox would give. Compare its output with yours. The full playground
Everything up to chapter 12, Classes. Later features are syntax errors here.
Run the code to see what it prints.jlox from Crafting Interpreters by Robert Nystrom, ported to TypeScript (MIT).
Your notes
Only youSign in to keep private notes on this guide, in Markdown. They stay yours: never on your profile, and you can export them all.
Discussion
0 postsQuestions about this chapter, tips, and what tripped you up. Hints over answers: full solutions go in a spoiler.
No posts yet. Ask a question or share a tip.
Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Reference
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.
Common traps
- Returning the wrong value from init() when called directly.
- Forgetting to bind this when a method is accessed but not immediately called.