Skip to content

Commit

Permalink
doc: update trait object syntax
Browse files Browse the repository at this point in the history
Close #358
  • Loading branch information
peter-jerry-ye committed Dec 16, 2024
1 parent 27d129b commit 28e9597
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions next/language/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ MoonBit features a structural trait system for overloading/ad-hoc polymorphism.

In the body of a trait definition, a special type `Self` is used to refer to the type that implements the trait.

### Super trait
### Extending traits

A trait can depend on other traits, for example:

Expand Down Expand Up @@ -239,7 +239,7 @@ Here's an example of calling trait `impl` with dot syntax:
MoonBit supports runtime polymorphism via trait objects.
If `t` is of type `T`, which implements trait `I`,
one can pack the methods of `T` that implements `I`, together with `t`,
into a runtime object via `t as I`.
into a runtime object via `t as &I`.
Trait object erases the concrete type of a value,
so objects created from different concrete types can be put in the same data structure and handled uniformly:

Expand Down
12 changes: 6 additions & 6 deletions next/sources/language/src/trait/top.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ test {
let duck1 = Duck::make("duck1")
let duck2 = Duck::make("duck2")
let fox1 = Fox::make("fox1")
let animals : Array[Animal] = [
duck1 as Animal,
duck2 as Animal,
fox1 as Animal,
let animals : Array[&Animal] = [
duck1 as &Animal,
duck2 as &Animal,
fox1 as &Animal,
]
inspect!(
animals.map(fn(animal) { animal.speak() }),
Expand All @@ -164,10 +164,10 @@ trait Logger {
}

trait CanLog {
log(Self, Logger) -> Unit
log(Self, &Logger) -> Unit
}

fn Logger::write_object[Obj : CanLog](self : Logger, obj : Obj) -> Unit {
fn &Logger::write_object[Obj : CanLog](self : &Logger, obj : Obj) -> Unit {
obj.log(self)
}

Expand Down

0 comments on commit 28e9597

Please sign in to comment.