Structs

A struct is an object which can hold a collection of variables and functions. A function inside a struct can access the other members of the same struct.

Basic layout

A simple example of struct usage:

SCENE main {
	/* Define a new struct type with two variables and one function */
	struct person {
		string name;
		int age;

		public int init(string _name, int _age) {
			name = _name;
			age = _age;
		};
		public int print() {
			echo "$name is $age years old\n";
		};
	};

	/* Create an instance of the struct and call the init()-function */
	person author->init("Atle", 25);

	/* Print the struct */
	author->print();

	/* Output of this program:
Atle is 25 years old
	*/
}

To access members of a struct, we use the element selection operator -> . The members we access from the outside must be marked with public. An error will ble issued if you attempt to access a variable or function which is not public. Members can optionally be preceeded with the private keyword.

Constructors and destructors

You may specify constructor- and destructor functions for structs. The constructor is always called at the location where an instance of a struct is declared. The destructor is called when the struct goes out of scope (when the block in which it is defined ends).

Constructors are typically used to initialize member variables of structs to make sure they always are ready for use. Destructors are used to cleanup or flush data when the object is not going to be used anymore.

Constructors can take arguments just like other functions. If you define arguments in a constructor, you have to call the constuctor with the same arguments at declaration.

The syntax for constructors and destructors is borrowed from C++. The constructor function is defined without any type, and its name must be the same as the structs name. The destructor is declared the same way, only with a tilde ~ in front of the name. You cannot return values from constructors and destructors, they are void functions.

As of now, you can only have one constructor per struct.

A struct with a constructor and destructor can look like this:

SCENE main {
	/* Struct with constructor and destructor */
	struct ctor_dtor {
		int value;

		/* Constructor with one argument */
		ctor_dtor (int _value) {
			value = _value;
			echo "C-tor called, value was $value\n";
		};

		/* Destructor for the struct */
		~ctor_dtor () {
			echo "D-tor called, value was $value\n";
		};
	};

	/* Declare an instance of struct 'ctor_dtor' and call the constructor */
	ctor_dtor cd_a(1);

	/* The destructor is magically called here, where the block ends */
}