Objects and inheritance
obj: families of types that share fields and behaviour, open and override, calling through a base type, and what is and is not enforced today.
Use obj when types form a family. A base marked open can be extended with :, and a method marked open fn can be replaced with override fn in a derived type. Calling a method on a value of the base type runs the version of the actual type.
open obj animal {
name: string
open fn speak(self) -> string = "..."
fn intro(self) -> string = "#(self.name): #(self.speak())"
}
obj dog : animal {
override fn speak(self) -> string = "Woof"
}
obj fish : animal { }
fn main() {
const a = dog { name: "Rex" }
const b = fish { name: "Nemo" }
print(a.intro())
print(b.intro())
}Rex: Woof
Nemo: ...Declaring a family
open obj base Ellipsiscan be extended; a plainobjis not meant to be.obj derived : base Ellipsisinherits the base's fields and methods and may add its own. Construct it with all fields, inherited ones included.open fnmarks a method that derived types may replace, andoverride fnreplaces it.
Using a derived value as its base
A value of a derived type can be used where the base is expected, and a list of the base type can hold both. A call runs the overriding method:
open obj a {
x: int
open fn f(self) -> int = 1
}
obj b : a {
override fn f(self) -> int = 5
}
fn main() {
const v: a = b { x: 1 }
print(v.f())
const w = [a { x: 1 }, b { x: 2 }]
for e of w { print(e.f()) }
}5
1
5obj compared with struct
struct | obj | |
|---|---|---|
| Inheritance | No | Yes, one base with : |
| Methods replaced by derived types | No | open fn and override fn |
==, print | Plain structs compare automatically | Need @derive or an impl; see eq |
| Copying | Plain data copies; copy otherwise | copy needs @derive(clone) or impl clone; see clone |
Reach for struct and interface first; they are simpler and cover most designs.
Deriving on an obj
@derive(eq, hash, clone)
obj point {
x: int
y: int
}
fn main() {
var seen: set<point> = set {}
seen.insert!(point { x: 1, y: 2 })
print(seen.contains(point { x: 1, y: 2 }), seen.len())
}true 1Not finished
abstractandfinalare accepted by the parser, but the checker does not enforce them yet: extending afinal obj, or anobjthat is notopen, and overriding a method that is notopen, all compile today. Do not rely on them for safety.super.method()in an override is not an expression yet.
Related
Enums
enum: cases with and without fields, constructing them, the leading-dot shorthand, switch and is, exhaustiveness, deriving show, and generic enums.
Interfaces
interface and impl: declaring shared behaviour, default methods, static dispatch with bounds, dynamic dispatch with any, and interfaces that build on others.