build-a-garbage-collector / lesson-03.md
Lesson 03 · The object model and heap

Reading and writing reference fields

Objects become a graph only when they can point at each other. Today you build the field accessors - SetField writes another object's id into a field, GetField reads it back - which is how every edge in the object graph is stored.

The goal

Store one object's id in another object's field and read it back.

Start here - the target
TO DO
Scenario: A field holds a reference to another object
Givena heap where a = New(2) and b = New(0), so a is id 0 and b is id 1
WhenSetField(a, 0, b) is called
ThenGetField(a, 0) returns 1 (the id of b)
Andthe untouched field GetField(a, 1) still returns the nil reference
Background

An object’s fields are references: each field either holds the nil reference or the id of another object. Writing b into field 0 of a records an edge in the object graph “a points at b” as a plain integer, the same way a real object would hold a pointer to another. SetField stores the id; GetField reads it back.

This is the whole vocabulary the collector needs. Reachability, marking, and copying will all be phrased in terms of “follow object a’s fields to the objects it references.” Note that a field you never set stays Nil - an object referencing nothing through that slot - which the collector will treat as a dead end when it traces. Next you will list the live objects, then gather the roots.

Make it work
func (h *Heap) SetField(obj Ref, i int, val Ref) {
h.slots[obj].fields[i] = val // store the referenced object's id in field i
}
func (h *Heap) GetField(obj Ref, i int) Ref {
return h.slots[obj].fields[i]
}
CheckpointDONE
Objects can reference each other through their fields. Commit and stop here.