Reflect

Structs

⚠️ TODO

Example

struct Vec2 {
	var x: float;
	var y: float;

	method getLength() => sqrt(this.x ^^ 2 + this.y ^^ 2);
}

var v = Vec2(3.0f, 4.0f);
print(v.getLength()); // 5

Constructor and Destructor

❌ Not Implemented

Copy and Move Operators

❌ Not Implemented

The behavior of a struct value being copied or moved by the compiler can be customizing by overloading the copied and moved operators.

The copied operator will be called whenever a value was copied to a new memory location. It allows the struct to perform bookkeeping tasks, such as incrementing a reference count.

The moved operator is called similarly after a value has been moved to a different memory location.

Both operators can be marked with the @disable attribute do disallow copying or moving values of the respective struct type.

Example

struct S {
	operator[copied] {
		print("copied!");
	}

	operator[moved] {
		print("moved");
	}
}

struct T {
	// disallows any value of type T to be moved
	@disable operator[moved];
}

Properties

❌ Not Implemented

Example

struct Widget {
	private {
		var _width: int;
		var _height: int;
		var _caption: string;
	}

	// read-only
	var width { get => _width; }
	var height { get => _height; }

	// read-write
	var caption {
		get => _caption;
		set(v) { _caption = v; redraw(); }
	}

	function redraw() {
		// ...
	}
}

Operating on Properties

Using modifying operators, such as assignment or increment operators, on a property will automatically generate code that uses the property getter and setter to implement the operation equivalent to operating on a plain value of the same type.

Example

struct MyArray {
	private {
		var _length: size_t;
		var _capacity: size_t;
		var _ptr: void*;
	}

	var length {
		get => _length;
		set(v) {
			if (v <= _capacity) {
				_length = v;
			} else {
				_ptr = realloc(_ptr, v);
				_length = _capacity = v;
			}
		}
	}
}

var arr: MyArray;

// normal setter call
arr.length = 10;

// modifying operator
arr.length += 5;
// translated to:
// [&arr] { var tmp = arr.length; tmp += 5; arr.length = tmp; return tmp; } ()