Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 8: Objects and classes

Objectives: LO-8.1, LO-8.2, LO-8.3
Prerequisites: the Module 8 reading and Lab 7
Time: about 2 hours

You will write a stack class, a subclass that limits its size, the same limited stack by composition, and a stack whose state hides in closures, and then measure what each kind of object costs. As in the earlier labs, each check cell prints passed or fails with a message, and the checks fail until you finish their exercise.

Setup

Section 5 measures time and memory, so its numbers depend on your machine. The second cell prints the Lua version, which should be Lua 5.4, and defines is_instance from the reading.

1. A class

Exercise L8.1 (LO-8.1, ★★, about 25 minutes)

Write the class Stack. Its objects hold any values except nil:

  • Stack.new() returns a new, empty stack.

  • s:push(v) adds v on top; s:pop() removes and returns the top value, or returns nil when s is empty; s:peek() returns the top value without removing it; and s:size() returns the number of values.

  • #s is the same as s:size(), and tostring(s) lists the values from the bottom up, as in Stack(1, 2, 3); an empty stack is Stack().

Keep the methods in Stack, not in each object.

Hint

Give each object its own list and count in Stack.new: { items = {}, n = 0 }. __len and __tostring go in Stack too, since Stack is every stack’s metatable.

2. A subclass

Exercise L8.2 (LO-8.2, ★★, about 25 minutes)

Write BoundedStack, a subclass of Stack whose objects hold at most limit values. BoundedStack.new(limit) returns a new, empty bounded stack. Its push raises the error "stack is full" at level 2 when the stack already holds limit values; every other method, #, and tostring work as for Stack. Don’t change Stack.

Hint

Follow the reading’s Savings. Build the object with Stack.new(), then give it the metatable BoundedStack. Remember which two metamethods a subclass must copy.

3. The same design by composition

Exercise L8.3 (LO-8.2, ★★, about 25 minutes)

Write LimitedStack, which does the job of BoundedStack by composition: each object holds a Stack in its field stack, and offers only push, pop, and size, forwarding each to the stack. LimitedStack.new(limit) returns a new, empty limited stack, and push raises "stack is full" at level 2 like BoundedStack’s. Then answer the question in the Markdown cell after the check.

Your answer here: an editor keeps an undo history that must never hold more than 100 changes, and must never be read except from the top. Would you build it from BoundedStack or from LimitedStack? Defend your choice in two or three sentences.

4. Private state

Exercise L8.4 (LO-8.3, ★★, about 20 minutes)

Write new_private_stack(), which returns a stack whose items and count live in closures, not in any table that the caller can reach. The object is a table of three functions, called with a dot: s.push(v), s.pop(), which returns nil when the stack is empty, and s.size().

5. What each kind of object costs

The next cell builds 10,000 counters of each kind, both with four methods, and measures the memory each takes and the time 5 million calls take. Before you run it, predict: which kind takes more memory per object, and about how many times more? Which call is faster? The cell takes about a second.

Both memory figures include the slot each counter takes in the list keep. Change n, or add a fifth method to both kinds, and run the cell again: the closure-based counter grows with every method, and the metatable-based one doesn’t.

Summary

  • A class is a table of methods that serves as its objects’ metatable; each object holds only its own data, and methods take the object as self.

  • A subclass falls back on its parent through __index, overrides by redefining, and must copy the metamethods it keeps, such as __len and __tostring.

  • Composition holds a part and forwards only the methods it chooses, so the object isn’t an instance of the part’s class, and exposes nothing else.

  • Closure-based objects hide their state completely, and their calls are faster, but every object pays for its own closures.

Next, do the Module 8 practice.