Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Struct Definition

Define a struct with the struct keyword.

Syntax:

struct StructName {
    ...
}

where:

  • StructName is 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:

  • args is 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 self object, which is how it can modify the newly created object.
    • The self object is automatically returned at the end of the constructor, however can be returned manually if required.

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 self object, 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.

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 self and old objects.
      • The self object is the new duplication and will be automatically returned
      • The old object is the old object and will not be copied out

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:

  • type is the type which the object will be converted to
  • ... is the as method body
    • The self object is avaliable inside the as method
    • You will need to initialize and return a new value with the specified type

Use an as method like this:

myobj as type

where:

  • myobj is an object with an as method which provides a conversion to type
  • type is the target type