Skip to content

Traits And Trait Methods

Traits are runtime protocols. They describe methods a script or host type can implement, and they support dynamic protocol-style dispatch without becoming Rust traits in script syntax.

Trait methods can be required signatures or default methods. Type hints on trait methods are runtime contracts and reflection metadata.

trait BonusSource {
fn bonus(self, amount: i64) -> i64 {
return amount
}
}

Use impl Trait for Type to implement a protocol for a script type. An explicit method overrides the trait default.

struct Player {
level: i64
}
impl BonusSource for Player {
fn bonus(self, amount: i64) -> i64 {
return self.level + amount
}
}

Trait method calls are still receiver-dispatched. Known receiver calls can use linked method IDs; dynamic calls resolve through the runtime receiver classification and registry-backed metadata.

Traits do not allow monkey patching or runtime mutation of type structure. Host type implementations must be registered by the host and must preserve HostAccess safety, capability checks, and hot reload compatibility.