Interfaces and impls
An interface declares method contracts. impl Interface for Type supplies them, after which the concrete value can be passed where the interface type is expected.
Calls through the interface use the implementation supplied for the value's type. Each impl must match the interface's parameter and return types exactly.
interface Describe {
fn describe(self: string) -> string
}
struct Dog { name: string }
impl Describe for Dog {
fn describe(self: Dog) -> string { "Dog: ${self.name}" }
}
fn print_item(item: Describe) { println(item.describe()) }
fn main() { print_item(Dog { name: "Rex" }) }
Examples use the moon toolchain version shown beside each result.
Output & diagnostics
Expected: Dog: Rex
Ctrl/⌘+Enter runs the current lesson.