Skip to content

Type Definitions

Defining Types

Use the type keyword to define a custom type:

type Point
{
float x;
float y;
}

A type definition describes the layout — its fields, methods, and behavior. How instances are stored in memory depends on how the type is used at the declaration site: as a value (Point), a GC reference (Point?/Point!), or a raw pointer (Point*/Point&). See Using Types below for details.

Constructors

Constructors use the type name as the method name:

type Point
{
float x;
float y;
Point(float x, float y)
{
this.x = x;
this.y = y;
}
}
Point p = Point(1.0, 2.0);

Shorthand Constructor

Define fields and a constructor in one line:

type Point(int x, int y) { }

Destructors

Destructors use ~TypeName and are called when a value goes out of scope or is otherwise cleaned up:

type FileHandle
{
int handle;
~FileHandle()
{
Close(this.handle);
}
}

Methods

type Vector2
{
float x;
float y;
float Length() Math.Sqrt(x * x + y * y);
Vector2 Normalize()
{
float len = Length();
return Vector2(x / len, y / len);
}
}

Static Members

Static fields and methods belong to the type itself, not instances:

type Counter
{
static int count = 0;
static void Reset() { count = 0; }
static void Increment() { count = count + 1; }
}
Counter.Increment();

Inheritance

Types can extend a single base type. Inheritance works with all type categories — value types, GC references (T?/T!), and raw pointers (T*/T&):

type Shape
{
string color;
}
type Circle : Shape
{
float radius;
Circle(float r)
{
this.radius = r;
this.color = "red";
}
}

Implicit Base Constructor

If a derived constructor omits an explicit : Base(...) chain, the compiler implicitly invokes the parent’s default (no-argument) constructor before the body runs:

type Animal
{
int legs;
Animal() { this.legs = 4; }
}
type Dog : Animal
{
string name;
Dog(string n)
{
// Animal() runs first, setting this.legs = 4
this.name = n;
}
}

This guarantees inherited fields are always initialized before the derived body sees them.

Type-Body Modifiers

unsafe and requires(caller: trusted) can sit on the type body itself, after the optional base list. They cascade to every method body in the type:

type Driver : Hardware requires(caller: trusted) unsafe
{
int* mmio;
void Poke(int offset, int value)
{
// 'unsafe' is implicit; 'requires(caller: trusted)' satisfies the
// FFI / extern call trust gate without per-method annotations
*(this.mmio + offset) = value;
}
}

This is the type-level equivalent of writing requires(caller: trusted) unsafe on every method, and is the recommended form when an entire type interfaces with native or trusted-only resources. Modifiers may appear in either order; both are non-template only.

Final and Abstract Types

final type Constant
{
int value;
}
abstract type Animal
{
abstract int Legs();
}
type Dog : Animal
{
override int Legs() 4;
}

Copy and Move Operators

Use : default and : delete to control copy/move behavior:

type Resource
{
void =(copy) : default; // auto-generated copy
void =(move) : delete; // disallow move
}

Using Types: Value, Reference, and Pointer

The same type definition can be used in three different ways depending on how you declare variables of that type. The storage, lifetime, and dispatch behavior are determined by the declaration site, not the type definition itself.

As a Value Type (Stack)

A plain type name stores the instance directly on the stack — copied on assignment, no GC involvement:

type Point { int x; int y; }
Point a;
a.x = 10;
Point b = a; // b is a copy of a
b.x = 99;
// a.x is still 10

Value types have no vtable pointer overhead. Fields are laid out directly with no header. Method calls are resolved statically at compile time.

As a GC Reference (Heap)

Append ? (nullable) or ! (non-null) to allocate on the garbage-collected heap:

type Node
{
int value;
Node? next; // nullable GC reference to another Node
}
Node! head = new Node(); // non-null, heap-allocated
head.value = 42;
head.next = null;

GC-allocated objects have extra metadata before the data area:

ComponentSizeDescription
GC header16 bytesMark bits, generation, size — managed by the collector
Vtable pointer8 bytesPoints to the type’s method table (always present for GC objects)
FieldsvariesInstance data, same field layout as defined by the type

The vtable pointer is always present on GC objects, even if the type has no virtual methods. It stores a pointer to the TypeInfo record, which the GC and reflection system use for type identification.

The garbage collector automatically frees unreachable objects.

As a Raw Pointer (Unsafe)

Inside unsafe blocks, use T* (nullable) or T& (non-null) for unmanaged pointers:

unsafe
{
Point* p = new* Point();
(*p).x = 10;
delete* p;
}

Raw pointers are not tracked by the GC. You are responsible for manual delete*.


Virtual Methods and Vtables

By default, method calls on types are statically dispatched — the compiler resolves which function to call at compile time. No vtable is needed.

A type gets a vtable when any of these conditions are met:

  • The type declares a virtual method
  • The type declares an abstract method
  • The type uses override on a method
  • The type inherits from a type that has a vtable

Static Dispatch (No Vtable)

type Greeter
{
string Greet() "Hello"; // non-virtual — resolved at compile time
}

No vtable pointer is stored in the instance. Greet() is called directly.

Virtual Dispatch (With Vtable)

type Shape
{
virtual float Area() 0.0;
}
type Circle : Shape
{
float radius;
override float Area() Math.PI * this.radius * this.radius;
}

When virtual or abstract methods are present, the compiler:

  1. Reserves 8 bytes at offset 0 of the instance for a vtable pointer
  2. Builds a vtable array containing function pointers for each virtual method
  3. At call sites, reads the vtable from obj[0] and dispatches through it

This applies to both value-type and GC-reference usage. A value-type variable of a type with virtual methods will also have the vtable pointer in its layout.

Abstract Types

Abstract types cannot be instantiated directly. They define the interface that derived types must implement:

abstract type Animal
{
abstract int Legs(); // must be overridden
virtual string Sound() "..."; // can be overridden
}
type Cat : Animal
{
override int Legs() 4;
override string Sound() "Meow";
}

Instance Layout Summary

ScenarioLayoutVtable pointer?
Value type, no virtual methods[fields...]No
Value type, has virtual methods[vtable_ptr][fields...]Yes (8 bytes at offset 0)
GC reference (T? / T!)[GC header (16B)][vtable_ptr (8B)][fields...]Always
Raw pointer (T* / T&)Same as value typeSame as value type