Skip to content

Object

Object is the universal root class. Every class you write without an explicit parent (every class X { … }) inherits from it implicitly. Its three methods are the defaults your own types get, and the ones a Map, Set or Array uses when you don’t override them.

#import "Foundation.xc" // Object comes in with the umbrella

You write nothing to inherit from Object: it is the implicit parent of any parentless class. An Object* accepts a pointer to any class you define, and any of your instances fits wherever an Object* is expected.

Its defaults are the cheapest correct implementations:

  • equals is pointer identity: two Object*s are equal only when they point at the same heap block.
  • hash folds the receiver’s address. Distinct instances live at distinct addresses, so they always hash apart.
  • description returns the placeholder <Object>.

The pointer hash is not cached on the instance. It takes two instructions, so a one-byte cache field on every object in the program would cost more memory than it saves in cycles. A class whose hash is expensive (a long String) can cache it in a private ivar of its own.

Override equals and hash together when your class needs value semantics rather than identity. Number compares by stored numeric value; String and Data fold over their bytes. The default hash is address-derived and varies between runs, which is why Map and Set iterate in insertion order rather than hash order.

  • Hashable: hash makes any object usable as a Map / Set key.
  • Comparable: equals is the required slot. The optional compare is not implemented (identity has no natural order), so plain Objects have equality but no ordering.

Every class is an Object, so every class has these protocol vtables and defaults from the moment it is declared.

Protocol methods · equals · hash · description


The complete public surface: the three hooks your classes inherit and override.

bool equals(Object* other)

Pointer identity: true only when self and other are the same heap block. This is the Comparable / Hashable equals slot, dispatched through the vtable every object carries. Override it (together with hash) to give your class value semantics.

u32 hash(void) // u8 on the xt6502 build

An XOR-and-multiply scramble of the receiver’s address (a plain XOR-fold of the low address bytes on the 6502). Distinct instances live at distinct heap addresses, so they always hash apart. This is the Hashable method, so any object can key a Map or Set with no extra work. It is not cached; see Overview.

String* description(void)

A String describing the object. The default returns the placeholder <Object>. Stdio.printf’s %@ conversion dispatches through this hook, so overriding it controls how your class prints: a Number renders its value, and a Data renders <Data 4: deadbeef>.

↑ Topics