Skip to content

Types & Methods

Defining a Type

Use type to define a custom type:

type Vec2
{
float x;
float y;
}

Constructors

Define constructors with the type’s name:

type Vec2
{
float x;
float y;
Vec2(float x, float y)
{
this.x = x;
this.y = y;
}
}
Vec2 v = Vec2(3.0, 4.0);

Shorthand Types

If your type is mainly a data container, use shorthand syntax — fields and a constructor in one line:

type Point(int x, int y) { }
Point p = Point(10, 20);
Console.WriteLine(p.x); // 10

Methods

Methods are defined inside the type body. Like functions, they can use block bodies or expression bodies:

type Vec2(float x, float y)
{
// Block body
float Length()
{
return Math.Sqrt(this.x * this.x + this.y * this.y);
}
// Expression body
float Dot(Vec2 other) this.x * other.x + this.y * other.y;
}
Vec2 v = Vec2(3.0, 4.0);
Console.WriteLine(v.Length()); // 5.0

Operator Overloading

Define operators directly with the symbol — no operator keyword needed:

type Vec2(float x, float y)
{
Vec2 +(Vec2 other) Vec2(this.x + other.x, this.y + other.y);
Vec2 -(Vec2 other) Vec2(this.x - other.x, this.y - other.y);
Vec2 *(float s) Vec2(this.x * s, this.y * s);
}
Vec2 a = Vec2(1.0, 2.0);
Vec2 b = Vec2(3.0, 4.0);
Vec2 c = a + b; // Vec2(4.0, 6.0)

Properties

Define computed properties with get and = (set):

type Circle
{
float radius;
float area
{
get { return Math.PI * this.radius * this.radius; }
}
float diameter
{
get { return this.radius * 2.0; }
=(v) { this.radius = v / 2.0; }
}
}

Heap Allocation (GC References)

A plain type name stores the instance on the stack. Use ? or ! with new to allocate on the GC heap instead:

type Node
{
int value;
Node? next; // nullable GC reference
Node(int value)
{
this.value = value;
this.next = null;
}
}
Node! head = new Node(1); // non-null GC reference
head.next = new Node(2); // nullable field

Next Steps

You’ve covered the basics. From here, explore the Language Reference for the full details, or check out the Standard Library.