Crafting Interpreters · Week 10

Ch 12: Classes

Updated

About 7 hoursVerified by tests

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.

Flashcards 6 cards for Anki and AnkiMobile · all decks

Anki deck.tsv

This week's steps

4 steps
  1. Read the chapter

    Implement in order and test after each step.

  2. Write the code in your lox repo

    Type it into jlox/ (Java) yourself as you read, and get it running.

  3. Do the challenges (1–3)

    Challenges 1 (static methods) and 2 (getter methods) are both good.

  4. 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

Optional

jlox 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

Chapter 12: Classes

Everything up to chapter 12, Classes. Later features are syntax errors here.

Output
Run the code to see what it prints.

jlox from Crafting Interpreters by Robert Nystrom, ported to TypeScript (MIT).

Your notes

Only you

Sign 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 posts

Questions 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.

Spot a mistake? Report it

Back to week 10