Skip to content

Interfaces

Defining Interfaces

Interfaces declare a contract of methods a type must implement:

interface IDrawable
{
void Draw();
int64 GetArea();
}

Implementing Interfaces

Use : to declare that a type implements an interface:

type Circle : IDrawable
{
float radius;
void Draw()
{
// render circle
}
int64 GetArea() Math.PI * radius * radius;
}

A type can implement multiple interfaces:

type Widget : IDrawable, IClickable { }

Virtual and Override Methods

Use virtual on base type methods and override in derived types:

type Shape
{
virtual int64 Area() 0;
}
type Square : Shape
{
int64 side;
override int64 Area() this.side * this.side;
}

Abstract Methods

Abstract methods have no body and must be overridden:

abstract type Animal
{
abstract int64 Legs();
}
type Dog : Animal
{
override int64 Legs() 4;
}

Interface References

Interface types act as references. Use ! (non-null) or ? (nullable):

IDrawable! shape = new Circle();
shape.Draw();
IDrawable? maybe = null;
maybe = new Circle();

Interface Inheritance

Interfaces can extend other interfaces:

interface IShape : IDrawable
{
float Area();
float Perimeter();
}

A type implementing IShape must implement all methods from both IShape and IDrawable.

Checking Interface Implementation

Use is to test whether a value implements an interface at runtime:

void Process(IDrawable! obj)
{
if (obj is ISerializable s)
{
s.Serialize();
}
}

Cross-Assembly Interfaces

Interface implementation lists are persisted in .asm, so a type Bar : IFoo declaration round-trips faithfully across assembly boundaries. Importing an assembly that declares Bar brings along the fact that Bar implements IFoo, even when IFoo itself was declared in yet another imported assembly. Vtables are filled in during linking on the consumer side, so dispatching through an IFoo! reference to a Bar instance reaches the correct override.

Interface-typed receivers also work end-to-end:

void Process(IDrawable d)
{
d.Draw(); // vtable indirection through the interface entry
}

The compiler synthesizes a backing type per interface to carry the dispatch shape; no copy of the original value is made.