Skip to content

Interfaces

Interfaces describe behavior. Any value with the required methods can satisfy an interface.

Use iface to describe the methods a value must provide:

fn sayHi(sayer: iface {
say(msg: string) void
}) void {
sayer.say("Hi")
}

Define a struct and add methods with impl:

struct SayStruct {}
impl SayStruct {
fn say(msg: string) void {
println(msg)
}
}
sayHi(SayStruct())

Use type to name an interface:

type helloer: iface {
sayHello() void
}

Named interfaces make function signatures easier to read:

fn interfaceTest(obj: helloer) void {
obj.sayHello()
}
type helloer: iface {
sayHello() void
}
struct User {
name: string
}
impl User {
fn init(self: User, name: string) void {
self.name = name
}
fn sayHello(self: User) void {
println(`Hi! I am ${self.name}!`)
}
}
const john = User("John")
interfaceTest(john)