In addition to the already built in types, such as Map
and Function
, you can also define your own types. This is done with the class
-keyword.
// Create a new class called "Wizard"
class Wizard {}
class Wizard {
// Create the property "name" of type "String"
name : String
// You can also use the assignment operator (:=) to set a default value
// In this case the type will automatically be set to "Bool"
killed_voldemort := false
// When the value is a function ("fn"), it will be treated as a method
talk := fn() {
// You can use the values from within the same class Instance with the keyword "self" (see more below)
IO::Println("My name is ", self.name)
}
}
To be able to use the class as a value, you need to create a class-instance with the keyword new
.
// A new "Wizard" is created and assigned to "ron"
ron := new Wizard()
// Update the wizards name
ron.name := "Ron"
// A call to the method
ron.talk()
This code together with the class definition above will print
My name is Ron
You can have multiple instances of the same class if you want, and each class will get it’s own copy of all methods and values
ron := new Wizard()
harry := new Wizard()
ron.name := "Ron"
harry.name := "Harry"
ron.talk()
harry.talk()
The result would be:
My name is Ron
My name is Harry