4.7 Embedding and overriding
There is no implementation inheritance. Reuse is through embed:
data Animal { name: str }
impl Animal {
open fn speak() -> str => "..."
fn hello() -> str => "I am {name}: {speak()}"
}
data Dog { embed Animal; breed: str }
impl Dog { override fn speak() -> str => "Woof, {super.speak()}" }
Dog{ name: "Rex", breed: "lab" }.hello() // "I am Rex: Woof, ..."embed Tmakes the fields and methods ofTavailable on the outer type (promotion).- Only a method declared in
Tasopen fncan be overridden. The override MUST have the wordoverrideand an identical signature;overridewithoutopenis an error. - A call of an
openmethod throughselfinside the methods ofTis dispatched to the override of the outer type. Non-openmethods are bound statically. super.m()calls the version of the embedded type; with several embeddings —super(Animal).m().- A conflict of promoted names of two embeddings is an error on use until the name is overridden or given explicitly (
self.Animal.name). Dogis not a subtype ofAnimal; access to the part isdog.Animal(&Animal).Dogsatisfies the interfaces thatAnimalsatisfies, through the promoted methods.
Related diagnostics
ambiguous-embed, missing-override, override-without-open
See also
Source: docs/spec/oxl-standard.en.md