Skip to content

Structs and traits

rockofox edited this page Jan 11, 2024 · 1 revision
struct Dog = (name: String)
struct Cat = (name: String)

let main => IO = do
    let bello = Dog { name : "Bello" }
    let mauzi = Cat { name : "Mauzi" }
    println name bello
    println name mauzi
end

The above example shows simple data structures. The name method gets created automatically. Name collisions (shoud be) resolved automatically.

trait Animal = do
    makeNoise :: Self -> IO
end

impl Animal for Dog = do
    makeNoise self = println "Woof"
end

impl Animal for Cat = do
    makeNoise self = println "Meow"
end

let main => IO = do
    makeNoise (Dog {})
    makeNoise (Cat {})
end

The above example defines a trait called Animal with a method makeNoise. Then, two implementations of the Animal trait are provided for the types Dog and Cat. The implementation for Dog specifies that when makeNoise is called, it will print "Woof," and the implementation for Cat will print "Meow."

Clone this wiki locally