Traits
Interfaces, implementations, Self, defaults, bounds, composition, and generic targets.
Contents
A trait declares a set of methods. Traits are statically resolved: there are no trait objects, dynamic dispatch, or runtime method tables.
Declaring a Trait#
A trait contains method signatures and optional default bodies. Methods may be marked pub and pure, and may carry contracts.
trait Eq {
pub fn eq(x, y Self) bool
pub fn neq(x, y Self) bool {
return !x.eq(y)
}
}
Self is the implementing type. The first parameter may omit its type, which means Self. Subsequent parameters must be written explicitly.
trait Number[T] {
pub fn add(x, y T) T
pub fn sub(x, y T) T
}
Implementing a Trait#
An implementation names the target type, then of, then the trait and any arguments.
impl i32 of Eq {
pub fn eq(x, y Self) bool => x == y
}
impl Account of Eq {
pub fn eq(x, y Self) bool => x.id == y.id
}
The target may be a built-in type, record, refinement, generic instantiation, or parameterized built-in such as List[i32]. The implementation may live in another module if the trait is visible.
trait Size {
pub fn size(x Self) i64
}
impl List[i32] of Size {
pub fn size(x Self) i64 => cast[i64](x.len)
}
impl str of Size {
pub fn size(x Self) i64 => cast[i64](x.len)
}
Contracts on trait method declarations are copied onto implementation methods, with parameter names mapped. An implementation may add further clauses.
Calling Trait Methods#
fn main() {
var left = Account { id = 1, balance = 100 }
var right = Account { id = 1, balance = 50 }
print(left.eq(right))
print(left.neq(right))
}
Method resolution first looks for a trait implementation matching the receiver type and method name. If none is found, the compiler falls back to a free function with the same name.
Default Methods#
A method with a body in the trait declaration is a default. An implementation may inherit or override it.
trait Counter[T] {
pub fn base(x T) i64
pub fn doubled(x T) i64 {
return x.base() * 2
}
pub fn reset(x T) T {
return Self { value = 0 }
}
}
type Box {
value i64
}
impl Box of Counter {
pub fn base(x Self) i64 => x.value
}
Box.doubled() calls the default, which calls the overridden base. Box.reset() constructs a Self value.
impl Box of Counter {
pub fn base(x Self) i64 => x.value
pub fn label(x Self) str => "custom"
}
Bounds#
A bound is written after the parameter name, separated by a space.
fn largest[T Ord](a T, b T) T {
if a.lt(b) {
return b
}
return a
}
The bound is checked when largest is instantiated. A parameterized bound uses brackets.
trait Convert[A] {
pub fn convert(x Self) A
}
fn read[T Convert[i64]](value T) i64 => value.convert()
Composition#
A trait may extend base traits. The colon introduces the bases; the composed trait inherits their declarations and default methods.
trait Eq {
pub fn eq(x, y Self) bool
pub fn neq(x, y Self) bool {
return !x.eq(y)
}
}
trait Ord : Eq {
pub fn lt(x, y Self) bool
pub fn largest(x, y Self) Self {
if x.lt(y) {
return y
}
return x
}
}
An implementation of Ord must provide lt and eq, and inherits neq and largest.
type Box {
value i64
}
impl Box of Ord {
pub fn eq(x, y Self) bool => x.value == y.value
pub fn lt(x, y Self) bool => x.value < y.value
}
fn same[T Ord](a T, b T) bool => a.eq(b)
Parameterized bases use explicit arguments, as in trait Derived[B] : Base[B].
Generic Traits#
A generic trait is declared with type parameters and implemented with concrete arguments. The target may also be generic.
trait Sum[T] {
pub fn sum(x, y T) T
}
impl Box[i64] of Sum {
pub fn sum(x, y Self) Self => Self { value = x.value + y.value }
}
When the trait has several parameters, write the arguments explicitly.
trait Convert[A] {
pub fn convert(x Self) A
}
impl Meter of Convert[Meter, i64] {
pub fn convert(x Self) i64 => x.value
}
The arity must match the trait declaration.
Refinements and Built-ins#
A refinement is a distinct target. An implementation for Positive does not apply to i64, and an implementation for i64 does not apply to Positive.
trait Ord {
pub fn lt(x, y Self) bool
}
type Positive = i64 : invariant self > 0
impl Positive of Ord {
pub fn lt(x, y Self) bool => x < y
}
An implementation for List[i32] applies only to List[i32]; a separate implementation is needed for List[str].
External Implementations#
The entry module may implement its own trait for an imported type.
use lib/trait_types {Quantity}
trait Sum[T] {
pub fn sum(x, y T) T
}
impl Quantity of Sum {
pub fn sum(a, b Self) Self => Self { value = a.value + b.value }
}
The target type must be visible and the trait must be in scope.
Static Resolution#
Trait calls are resolved at compile time. There is no dyn type, virtual table, or runtime interface. Generic code is monomorphized.
A heterogeneous collection is modelled with a sum type whose variants carry the concrete types. See Sum Types and Enums.
Summary#
| Feature | Rule |
|---|---|
| Declaration | trait Name { fn method(...) ... } |
| Self | The implementing type; first parameter may omit its type |
| Implementation | impl Target of Trait[args] { ... } |
| Default method | A method with a body in the trait declaration |
| Override | Provide the method again in the implementation |
| Bound | T Trait or T Trait[args] on a function type parameter |
| Composition | trait Derived : Base, Other |
| Built-in target | Allowed, including parameterized targets |
| Refinement target | Allowed; distinct from the base type |
| Dynamic dispatch | Not supported |
| Associated types | Not supported |