-
Notifications
You must be signed in to change notification settings - Fork 0
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."
- Home
- Functions and bindings
- Comments
- Structs and traits
- FFI
- Generics
- Imports/Modules
- Partial Functions
- Type System
- Pattern Matching
- Operators