OxL Reference

4.7 Embedding and overriding

There is no implementation inheritance. Reuse is through embed:

oxl
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 T makes the fields and methods of T available on the outer type (promotion).
  • Only a method declared in T as open fn can be overridden. The override MUST have the word override and an identical signature; override without open is an error.
  • A call of an open method through self inside the methods of T is dispatched to the override of the outer type. Non-open methods 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).
  • Dog is not a subtype of Animal; access to the part is dog.Animal (&Animal). Dog satisfies the interfaces that Animal satisfies, through the promoted methods.

ambiguous-embed, missing-override, override-without-open

See also

Source: docs/spec/oxl-standard.en.md