Classes
A class describes something you can make instances of: the data each one holds, and what it can do.
class Lamp
let lamp = Lamp()
Constructor parameters as fields
Marking a constructor parameter with let or var makes it a field, fixed or settable. This is the most common way to
write a class:
class Person(
let firstName: string,
let lastName: string,
var age: i32
) {
let sayHello() {
println("Hello ${firstName} ${lastName}")
}
}
entrypoint main() {
let person = Person("Ada", "Lovelace", 36)
person.sayHello()
person.age = 37
}
A parameter without let or var is passed to the constructor and not kept.
Writing the constructor
For more control over construction, write the constructor yourself with new. Inside a class, self is the instance:
class Person {
let first : string
let last : string
let age : i32
new(first: string, last: string, age: i32) {
self.first = first
self.last = last
self.age = age
}
}
Members
A class's members are its bindings: its fields and its functions. Inside the class they're read by name, or through
self. Outside, through an instance:
class Counter {
var count = 0
let increment() {
count = count + 1
}
}
entrypoint main() {
let counter = Counter()
counter.increment()
println("${counter.count}")
}
private keeps a member inside its class, and static makes a member belong to the class rather than to each
instance.
Traits, not inheritance
A class can implement traits, written after a colon, which is how types share a contract. A class inheriting from another class is not in V0 yet.