Struct Definition
Define a struct with the struct keyword.
Syntax:
struct StructName {
...
}
where:
StructNameis the name of your struct...is the body of your struct
Struct Body
Inside the struct body, you can:
- Define fields
- Define methods
- Define special methods (
constructor,destructor,duplicator,as)
Defining fields
A field in a struct is defined in the same way as a variable.
struct MyStruct {
x = 5
}
This creates a field named x with a default value of 5.
Defining methods
A method in a struct is defined in the same way as a named function.
struct MyStruct {
def myMethod() int {
return 0
}
}
Methods have access to the struct’s members through the self object, which is implicitly passed.
struct MyStruct {
x = 10
def myMethod() int {
return self.x * 2
}
}
Private and protected fields
A private field is a field which only methods inside the object can read and write.
A protected field is a field which only methods inside the object can write to, however anywhere else in the program the field can be read.
All other fields are public, meaning anywhere in the program can read or write these fields.
Methods and special methods are by default protected, however can be made private.
struct MyStruct {
private x = 10
protected y = "Hi"
private doSomething() int {
return 14
}
}
Defining special methods
In Solstice, objects have some special methods which provide ease of use while handling these objects.
Constructor
A constructor is used to initialize an object with user-provided fields.
Define a constructor like this:
struct MyStruct {
constructor(args) {
...
}
}
where:
argsis multiple comma-seperated pairs of:- a type identifier for the parameter, and
- an identifier (representing the parameter name)
...is the constructor body- The constructor body has access to the
selfobject, which is how it can modify the newly created object. - The
selfobject is automatically returned at the end of the constructor, however can be returned manually if required.
- The constructor body has access to the
Use a constructor like this:
MyStruct(args)
where:
- args is many comma-seperated expressions corresponding to the signature of the defined constructor
Destructor
A destructor is used to destroy an object once it goes out of scope.
Define a destructor like this:
struct MyStruct {
destructor {
...
}
}
where:
...is the destructor body- The destructor body has access to the
selfobject, which is how it can access and modify the object which is being destroyed. - There is no need to return a value from the destructor.
- The destructor body has access to the
Note how the destructor does not take any arguments.
Duplicator
A duplicator is used to copy an object when it is assigned to a new name, or passed to a function.
Define a duplicator like this:
struct MyStruct {
duplicator {
...
}
}
where:
...is the duplicator body- The destructor body has access to the
selfandoldobjects.- The
selfobject is the new duplication and will be automatically returned - The
oldobject is the old object and will not be copied out
- The
- The destructor body has access to the
Note how the duplicator does not take any arguments.
As methods
As methods allow easy conversion between different types.
Define an as method like this:
struct MyStruct {
as type {
...
}
}
where:
typeis the type which the object will be converted to...is the as method body- The
selfobject is avaliable inside the as method - You will need to initialize and return a new value with the specified type
- The
Use an as method like this:
myobj as type
where:
myobjis an object with an as method which provides a conversion totypetypeis the target type