Classes
Zap supports heap-only class types allocated with new.
Defining a class
Section titled “Defining a class”class Counter { priv value: Int;
fun init(value: Int) { self.value = value; }
pub fun inc(step: Int) Int { self.value = self.value + step; return self.value; }}Creating instances
Section titled “Creating instances”var counter: Counter = new Counter(10);init(...) acts as the initializer. self is available inside instance methods.
Visibility
Section titled “Visibility”Visibility modifiers:
pubprivprot
Inheritance and dynamic dispatch
Section titled “Inheritance and dynamic dispatch”Single inheritance is supported:
class Base { pub fun value() Int { return 1; }}
class Derived : Base { pub fun value() Int { return 2; }}Methods dispatch dynamically through the base type.
Another example
Section titled “Another example”class Task { prot done: Bool;
fun init() { self.done = false; }
pub fun finish() { self.done = true; }}Lifecycle hooks
Section titled “Lifecycle hooks”Classes can define:
init(...)for initializationdeinit()for cleanup on final release
Weak references and ARC details are covered in ARC and weak refs.