Default methods and bounds
An interface method may include a default body. An impl inherits it unless it provides an override. Defaults may call other methods from the same contract.
T: Interface constrains a generic parameter and makes those methods available in the body. Join several required interfaces with +.
interface Greet {
fn name(self: string) -> string
fn greeting(self: string) -> string { "Hello, ${self.name()}!" }
}
struct Person { value: string }
impl Greet for Person {
fn name(self: Person) -> string { self.value }
}
fn welcome<T: Greet>(value: T) -> string { value.greeting() }
fn main() { println(welcome(Person { value: "Ada" })) }
Examples use the moon toolchain version shown beside each result.
Output & diagnostics
Expected: Hello, Ada!
Ctrl/⌘+Enter runs the current lesson.