Skip to content

Mask Types

Overview

Mask types add methods and properties to existing types without modifying them. They cannot be instantiated, cannot have fields, and cannot have constructors or destructors.

Basic Syntax

namespace Core
{
type Point
{
int _x;
int _y;
int X { get this._x; =(v) this._x = v; }
int Y { get this._y; =(v) this._y = v; }
}
}
namespace PointExtensions
{
using Core;
mask type Point
{
int Sum { get this.X + this.Y; }
int DistanceSquared()
{
return this.X * this.X + this.Y * this.Y;
}
}
}

Using Extensions

Extensions are discovered through using directives. The original type’s members always take priority:

using Core;
using PointExtensions;
Point p;
p.X = 3;
p.Y = 4;
int s = p.Sum; // 7
int d = p.DistanceSquared(); // 25

Primitive Type Extensions

Mask types work on built-in types like int, float, and string:

namespace IntExt
{
mask type int
{
int Abs() { return this < 0 ? -this : this; }
int Clamp(int min, int max)
{
return this < min ? min : (this > max ? max : this);
}
}
}
using IntExt;
int x = -42;
int y = x.Abs(); // 42
int z = x.Clamp(0, 100); // 0

Static Extension Methods

namespace IntUtils
{
mask type int
{
static int Parse(string s) { return 0; }
}
}
using IntUtils;
int x = int.Parse("42");

Rules

  • No fields allowed — extension methods only
  • No constructors or destructors
  • this refers to the target type instance
  • Original type members take priority over extensions
  • Requires a using directive for the mask’s namespace