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 umbrellaOverview
Section titled “Overview”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:
equalsis pointer identity: twoObject*s are equal only when they point at the same heap block.hashfolds the receiver’s address. Distinct instances live at distinct addresses, so they always hash apart.descriptionreturns 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.
Conforms to
Section titled “Conforms to”Hashable:hashmakes any object usable as aMap/Setkey.Comparable:equalsis the required slot. The optionalcompareis not implemented (identity has no natural order), so plainObjects 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.
Topics
Section titled “Topics”Protocol methods · equals · hash · description
Protocol methods
Section titled “Protocol methods”The complete public surface: the three hooks your classes inherit and override.
equals
Section titled “equals”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 buildAn 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.
description
Section titled “description”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>.