Interfaces and implementations#
An interface is an operation contract: a type satisfies it by having methods with the declared operations and signatures. Use it when callers should depend on behavior, not a data type. Signatures follow the ordinary function rules .
Save as adjust.ms and run ms run adjust.ms:
interface Increment {
fn apply(value Int) Int
}
type Offset {
delta Int
}
fn Offset.apply(value Int) Int {
return value + self.delta
}
fn adjusted(value Int, operation Increment) Int {
return operation.apply(value)
}
fn main() Void {
print(adjusted(10, Offset(2)))
}12adjusted knows the operation it can call. Offset supplies it with an ordinary method, and another type can supply a different one without changing adjusted.
Methods#
A type’s methods are declared beside it, never inside the type. A receiver method is fn Type.name(...); one that changes self is fn var Type.name(...). A generic type repeats its parameters: fn Box[T].get() T. A method may declare type parameters of its own
.
self is an implicit binding of the receiver type in each method. It is not a parameter and cannot be redeclared.
A method is declared in the module that declares its type, and it travels with the type wherever the type is used. To add operations to another module’s type, write a free function or a wrapper type. There is no inheritance and no default implementation. A free function fn reset(counter Counter) is called as reset(counter), never counter.reset().
A type has one method per name. A second method of the same name on one receiver is an error, so one close or one read serves every interface that asks for it.
A receiver may add bounds, as in fn Box[T Equal].same(other T) Bool, so the method exists only where the arguments satisfy them. It may also fix an argument, as in fn List[Int].sum() Int. Methods of one name on receivers with different bounds or arguments are separate declarations, and a call picks the one whose receiver matches.
A method may add defaults to its parameters. A call on the concrete receiver uses them. A call through an interface value uses the interface’s signature, with the interface’s own defaults.
Satisfying an interface#
A type satisfies an interface when it has one own method for each operation, with the same name, parameter labels, var marking, parameter types, and result type. The type declares nothing extra: its methods are the declaration. For a generic interface, the methods must match exactly after the generic arguments are substituted.
- Changing the receiver. An operation that changes its receiver is
var fnin the interface andfn var Type.nameon the type. A method that does not changeselfalso meets it. varparameters. An operation that changes a caller’s value declares the parametervarin both places; the call writes the argument plainly, as for any function .moveparameters and receivers. An operation that takes its argument declares itmovein both places. A method that only reads the argument still writesmove(moving into a function ).- Returning a part. An interface operation that returns a borrowed part of its receiver or arguments declares
holdand&in both the interface and implementing methods (see borrowing ). - Labels. An implementation keeps the interface’s argument labels, even though labels are not part of function-value type identity .
- A mismatch is not an error until the type is used as the interface; the diagnostic lists the first operation that is missing or differs.
- A type with
fn move close() Void!satisfiesCloseableand is closed at scope end, so a type that is also aSourceand aSinkcloses once. Comparable,Equal,Hashable,Stringable, andJsonEncodeare met by an owncompare,equals,hash,to_string, orto_jsonof the exact signature.- A type or enum that meets
Stringableuses thatto_stringas its text form in interpolation,print, and containers. Ato_string()that does not returnString, or ato_string(prefix)with parameters, is an ordinary method, and the type prints structurally. - The language calls these methods on values it only borrows, so such a method may not keep its receiver or argument by passing it on with
move, storing it, or returning it. Keep a copy instead, such askeep(self.clone()). ClonableandDataare properties the compiler derives from a type’s fields; they are not declared.
- A type or enum that meets
- Operations with type parameters of their own are met by a method that repeats them with the same bounds (generic operations ).
// Rejected: amount does not match the interface's value label.
fn Offset.apply(amount Int) Int {
return amount + self.delta
}Coherence and visibility#
A method is available wherever its type is used, and satisfying an interface follows from the methods, so the result is the same in every module. There are no per-module extensions: a module cannot add methods to a type it imports.
A Map or Set always uses its key type’s own equals and hash, or structural equality when the type has none.
- Receivers must be source-defined types or enums declared in the same module. The standard library gives its own types and the built-in scalars their methods the same way.
- Interfaces may be generic but have at least one operation and no fields, cases, default bodies, associated types, or static operations.
- Exported interfaces expose every operation, under the public surface rules .
- A type exports its methods by being exported; a method is never exported by itself.
Generic receivers#
A receiver names its declaration’s generic parameters; matching the receiver determines every argument.
interface ReadValue[T] {
fn get() T
}
type Box[T Clonable] {
value T
}
fn Box[T].get() T {
return self.value.clone()
}This covers Box[Int], Box[String], and every other well-formed application. Bounds are inherited from the receiver declaration and from any bound written on the receiver. The body is checked once under them with self Box[T], and they must prove every nested application and operation legal. At a use, the matched arguments must satisfy them.
A bound written on the receiver restricts one method without restricting the data type:
interface SameValue[T Equal] {
fn same(other T) Bool
}
type Box[T] {
value T
}
fn Box[T Equal].same(other T) Bool {
return self.value == other
}A Box[T] without T Equal has no same. A receiver may also fix an argument, fn Box[Int].total() Int; it applies to Box[Int] only. Two receiver forms that could match the same application and declare one name are rejected, as are two methods of one name on the same form.
Generic operations#
An interface operation and a method may declare type parameters after their name. The method repeats the operation’s parameters in the same order with the same bounds, and its signature must match once they are renamed.
interface Ranked[T] {
fn best_by[K Comparable](key fn(T) K) T?
}
fn Scores.best_by[K Comparable](key fn(Int) K) Int? {
...
}A call infers the operation’s parameters from its arguments, or takes them explicitly: scores.best_by[Int](key). Dispatch is static and each use is monomorphized, so the operation works on a concrete receiver and through a type parameter bounded by the interface.
An interface with a generic operation cannot be the type of a value, because a method table has no entry for an operation whose types are not yet known. Converting to it is rejected. The standard List uses generic operations for max_by and min_by (src/stdlib/collections/collections.ms).
Interface conversion#
A value whose type satisfies an interface converts implicitly wherever that interface is expected: arguments, := with a type annotation, fields, collection entries, and returns. The type’s own methods match every operation
; a method name alone never converts.
A value that is already a view of the same interface application is unchanged. Other interface-to-interface conversions and unchecked downcasts are rejected. A contract type match can recover the concrete receiver with a runtime nominal check.
The conversion follows the ownership rules
of its position. A read-only argument borrows the operand, so the operand stays usable afterwards. A stored position (field, collection entry, :=, return) moves the operand at its last use, or needs .clone() otherwise.
A borrowed operand becomes a view: the interface value points at the operand without copying it, and the operand stays unchangeable while the view is used (references
). Only an interface with a var fn operation copies a borrowed operand, since a call through it may change the value.
fn use_increment(operation Increment) Int {
return operation.apply(1)
}
offset := Offset(2)
result := use_increment(offset)
again := offset.delta
kept Increment := offset.clone()An interface is never constructed: Increment(value: offset) is rejected, so annotate the binding (view Increment := offset) when no interface is otherwise expected. Generic inference does not convert: a type variable bound to Offset does not match Increment (matching
).
Selecting an operation#
A type has one method per name, so a call on a concrete receiver, a generic parameter or an interface value names one operation. Interfaces that declare the same name and signature are met by that one method. A bound method keeps its receiver and has the operation’s ordinary function type.
An interface type satisfies its own exact generic bound: Converter[Int] satisfies T Converter[Int], not T Converter[String].
fn invoke[T Increment](operation T) Int {
view Increment := operation
return view.apply(1)
}
from_concrete := invoke(offset)
from_view := invoke(view)Both calls satisfy the bound without implicit conversion.
Interfaces and services#
Pass a service as an interface value when a function needs storage, networking, or a clock. An interface can also serve as a generic bound that names the operations a type argument must provide.