Class
A class is simply a structure of data. It's used to accompany values and associated methods.
Defining and using a struct is easy and expressive. Let's define a struct of a person with their name, age and email.
struct Person {
name = "";
age = 0;
email = "";
}
What you might notice is that there is no need to specify a type, it is required to specify a default value and from that the type is inferred. Now it's possible to create an instance of this class by calling it like a function.
// Creates an empty Person with the default values
john := Person()
A struct is type safe and it's values can never be null. By default the values are set to their most basic variant.
john.name // evaluates to an empty string ""
john.age // evaluates to a zero 0
john.email // evaluates to an empty string ""
This feature is currently unimplemented
It's also possible to specify values to "construct" a class with. We're using the "Person" struct from the last few examples.
// Notice the curly braces initializing the values
jane := Person({ name: "Jane", age: 52, email: "[email protected]" })
// It's also possible to initialize some values (no age, so that will be 0 by default)
jane := Person({ name: "Jane", email: "[email protected]" })
Besides this you can also specify a constructor method in your struct if you prefer different default values, or to do logic on inputs.
class Person {
name = "";
age = 0;
email = "";
// Parameterless constructor
// Allows the above constructing to work with curly braces
constructor() {
if self.name == "Jane" {
print("Hi Jane!")
} else {
print("Hey unknown!")
}
}
}
// Notice that curly braces are still allowed
// This also prints "Hi Jane!"
jane := Person({ name: "Jane", email: "[email protected]" });
If you create a constructor with parameters, it's no longer possible to pass a hashmap as argument to initialize the struct. You then have to follow the constructors parameters.
class Person {
name = "";
age = 0;
email = "";
// Parameter constructor
// Doesn't allow constructing with a hashmap
constructor(string name) {
self.name = name;
self.age = 30;
self.email = "[email protected]"
}
}
// Notice there are no curly brackets
jane := Person("Jane")
This feature is currently unimplemented
While we just mentioned the first called method in a struct (the constructor). You can define your own methods to call on your structs. We're again using the "Person" struct, however now without a constructor. Methods can take parameters and also return values just like functions.
The simplest type of methods are the ones without parameters, let's start with a simple greet method that greets the person.
class Person {
name = "";
age = 0;
email = "";
// Define a new method named greet without any parameters
greet() {
print("Hello " + self.name + "!")
}
}
To call this method you first need to instantiate a new variant of the struct and then call it on that.
// New instance of Person with the name "Sam"
sam := Person({ name: "Sam" })
// Prints "Hello Sam!"
sam.greet();
Besides this simple method you can also do something more advanced, like modifying it's values, performing logic and returning values. Like if someone ages up check if they're at the retirement age of 65 and return true or false depending on if they are at that age.
class Person {
name = "";
age = 0;
email = "";
// Define a new method named ageUp
ageUp() {
// Increases the age
self.age += 1;
// Check if the age is higher than or equals to 65
if self.age >= 65 {
// If the age equals 65, congratulate them with their retirement
if self.age == 65 {
print("Congratulations on your retirement, " + self.name + "!")
}
// Return that they are indeed at the retirement age
return true
}
// Return that they are not at the retirement age
return false
}
}
ash := Person({ name: "Ash", age: 63 });