Bounds
An unconstrained type parameter promises nothing: the body may store, pass and return a T, but cannot call a method on it. A bound makes the promise. T: Scored admits only type arguments that provide the methods of the interface Scored, and in return the body may call those methods on any T.
Syntax
<T: Interface>
<T: First + Second + …>
<T: Interface, U>
A bound follows its parameter after a colon; several bounds are joined with +. A comma always starts the next type parameter, so <T: Scored, Display> declares a second parameter named Display rather than a second bound. Bounds may appear on the type parameters of functions, methods and generic types.
interface Scored {
func Score() -> int32;
}
interface Named {
func Name() -> char8[..];
}
func Best<T: Scored>(first: T, second: T) -> T {
if second.Score() > first.Score() {
return second;
}
return first;
}
func Announce<T: Scored + Named>(value: &T) {
PrintLine("{} has {}", value.Name(), value.Score());
}
Inside the body
A bounded T is exactly what its bounds say, and nothing more. Every requirement of every bound is callable on it; nothing else is:
error: no interface bound on type parameter 'T' provides method 'Score'
note: type parameter 'T' has no interface bounds
help: add a bound whose interface declares 'Score', as in 'T: SomeInterface'
The same holds for passing a T where an interface is expected: a T prints with {} only under a Display bound. Inside the body, Self in a bound's requirements stands for the type argument, so with Core::Equatable as the bound, left.Equals(right) takes another T — and a requirement returning Self returns a T.
Results stay concrete. Best called with two Player values returns a Player, with every field and method of Player available — it never becomes "some Scored".
Satisfying a bound
A type argument satisfies a bound when it has every method the interface requires, each taking and returning exactly what its requirement says, with Self standing for the type — a type whose methods have the right names but the wrong signatures does not satisfy it. Implementing the interface with extend T : Scored { … } is the usual way to provide them, but a bound is structural: a type whose plain extend block declares the required methods satisfies it too.
struct Player {
name: char8[..];
points: int32;
}
extend Player : Scored {
func Score(&self) -> int32 {
return self.points;
}
}
extend Player : Named {
func Name(&self) -> char8[..] {
return self.name;
}
}
struct Team {
total: int32;
}
// No ': Scored', but Score is there, so Team satisfies the bound.
extend Team {
func Score(&self) -> int32 {
return self.total;
}
}
Interface values are stricter: only a declared implementation converts to one. An interface that requires nothing, such as Core::Iterator, is satisfied by every type and only documents intent.
Generic interfaces
A bound may name a generic interface with its type arguments, which may be the bounding declaration's own parameters. A type parameter that appears only in a bound's type arguments is deduced from what the bounded argument's method returns or takes, so with Core's interface Cursor<T> { func Next(&var self) -> T?; }, a call to func Map<I: Cursor<T>, T, U>(source: I, transform: func(T) -> U) needs no type arguments written: a Counter whose Next returns int32? makes T an int32. It satisfies Cursor<int32> and not Cursor<int64>, whether or not it says so in an extend.
Primitives
A primitive type has no methods of its own, so it satisfies a bound only once an extend block gives it the methods:
extend int32 : Scored {
func Score(&self) -> int32 {
return self;
}
}
After this Best<int32>(3, 9) is accepted, while Best(2.5, 1.5) is not — float64 has > but no Score. Note that bare literals make T an int, not an int32: Best(3, 9) fails naming 'int', and the explicit <int32> is what makes 3 and 9 int32 values.
Copyable
Core::Copyable and Core::Sendable are the interfaces the compiler decides for itself; no extension implements either. A type satisfies Copyable when it is plain data, as Copy and move defines it, and a type parameter only when bound by it. Generic code copies a named T only under this bound, and the bound refuses a move-only type argument at the call:
error: type argument 'Owner' does not satisfy interface bound 'Copyable' on type parameter 'T'
note: 'Owner' owns resources, so it moves instead of copying
Core::Cloneable, by contrast, is an ordinary interface with one requirement, func Clone(&self) -> Self;, so T: Cloneable lets a body duplicate an owning value with value.Clone().
Sendable
Every type satisfies Sendable except a reference and a type that is or holds one declared #Unsendable, such as the standard Rc, whose count is not atomic. A raw pointer satisfies it, since what a pointer shares is the program's to vouch for, and a type parameter satisfies it only when bound by it. Thread::Spawn bounds a thread's context and result by it, so handing an Rc to another thread is refused:
error: type argument 'Rc<uint64>' does not satisfy interface bound 'Sendable' on type parameter 'T'
note: 'Rc<uint64>' is declared '#Unsendable', so it stays on the thread that made it
note: type parameter 'T' of function 'Spawn' is bound by 'Sendable'
help: keep the value on the thread that made it, or share it through a type made for threads, such as 'Arc' in place of 'Rc'
Checked at each use
A bound is checked where a type argument meets it — at each call of a bounded function or method, and at each written instantiation of a bounded type — never somewhere inside the body. The type arguments are inferred first, then every bound is checked on its own, and the failing one is reported with the missing method:
error: type argument 'float64' does not satisfy interface bound 'Scored' on type parameter 'T'
note: interface 'Scored' requires method 'Score', which type 'float64' does not implement
note: type parameter 'T' of function 'Best' is bound by 'Scored'
help: implement the interface, as in 'extend float64: Scored { ... }'
A type that meets one bound of Scored + Named but not the other is refused just the same.
Bounds travel
Inside a generic body, a type parameter satisfies exactly the bounds it was declared with. A generic that calls a bounded generic must therefore promise at least what the callee demands:
func Margin<T: Scored>(first: T, second: T) -> int32 {
let winner = Best(first, second); // T is Scored here, as Best requires
return winner.Score() * 2 - first.Score() - second.Score();
}
Without the bound on Margin, the call to Best is refused:
error: type argument 'T' does not satisfy interface bound 'Scored' on type parameter 'T'
note: type parameter 'T' is not constrained by 'Scored'
note: type parameter 'T' of function 'Best' is bound by 'Scored'
help: add the bound to the enclosing declaration, as in 'T: Scored'
Static dispatch
Each instantiation calls the method of its own type argument directly, resolved when it is compiled: no interface view or handle is made, and nothing is looked up at run time. The comparison with dynamic dispatch through a view or handle is tabled in Interface values.
See also
- Interfaces — declaring the interfaces bounds name
- Generics — type parameters and inference
- Interface values — the dynamically dispatched alternative
- Learn: Generic bound, Multiple bounds