Interfaces
Interfaces describe behavior. Any value with the required methods can satisfy an interface.
Inline Interfaces
Section titled “Inline Interfaces”Use iface to describe the methods a value must provide:
fn sayHi(sayer: iface { say(msg: string) void}) void { sayer.say("Hi")}Structs Implementing Interfaces
Section titled “Structs Implementing Interfaces”Define a struct and add methods with impl:
struct SayStruct {}
impl SayStruct { fn say(msg: string) void { println(msg) }}
sayHi(SayStruct())Named Interfaces
Section titled “Named Interfaces”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()}Example
Section titled “Example”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)