sysl

Traits

The one polymorphism mechanism — declaring, implementing, coherence, associated types, required traits, and trait objects.

sysl has one polymorphism mechanism: the trait. It is nominal: a type participates in a trait only through an explicit impl Trait for Type, never because its method names happen to match. Both dispatch strategies come from this one mechanism: static dispatch through a generic bound, and dynamic dispatch through a trait object.

trait Device
    name(self) -> string
    describe(self) -> string = "device " + self.name()

struct Pump
    tag: string

impl Device for Pump
    name(self) -> string = self.tag

var p = Pump("P-101")

print(p.describe())
device P-101

The example shows two things. A trait’s member is either a bare signature, which an implementation must supply, or a signature with a body, a default every implementation inherits unless it writes its own. And an implementation’s members become the type’s own, so p.describe() is called exactly as an inherent method is.

Declaring a trait

A trait’s members are the same kinds a type’s members are, declared and distinguished the same way:

memberformreached as
methodarea(self) -> intx.area()
propertysize -> intx.size, with no parentheses
a property’s setterset size(n)x.size = n
associated functionzero() -> SelfT.zero(), through the type
static propertystatic lowest -> intT.lowest, through the type and with no parentheses

A trait requires a property by declaring it without a body, and an implementation supplies it by writing the body:

trait Sized
    size -> int

struct Box
    w: int
    h: int

impl Sized for Box
    size -> int = self.w * self.h

var b = Box(3, 4)

print(b.size)
12

A property dispatches exactly as a method does: it has a receiver that is never written, so it takes a table slot beside the methods, and a bound allows reading it just as it allows calling them.

A trait may also require the setter, declared beside the property (declarations). A setter is a *self method that mentions Self only in its receiver, so it takes a slot of its own and does not affect object safety. Both a bound and an erased object can write the property:

trait Counter
    count -> int
    set count(n)

struct Cell
    v: int

impl Counter for Cell
    count -> int = self.v
    set count(n)
        self.v = n

bump[C: Counter](c: *C)
    c.count += 1

var cell = Cell(41)

bump(&cell)

var erased: &Counter = Cell(0)

erased.count = 7

print(cell.count, erased.count)
42 7

An implementation supplies both halves, or inherits either as a default; a block supplying only the setter need not restate the property. A property reached through a bound or a table belongs to the trait, so its setter is declared there. Writing a setter the trait does not declare is reported against the trait rather than the concrete type, because an inherent setter would not allow the write.

The kind of a member must match between the trait and the implementation. This check matters: a property and an associated function both lack a receiver to compare, so without it size(self) -> int could stand in for size -> int. A static property is a third kind with no receiver, and the static keyword distinguishes it from the other two.

An implementation may take by value what the trait takes by address. A trait’s *self states what every caller must be able to pass: an address, so a generic body or a table can let the member write through it. An implementation that needs less may ask for less. Written self, the call passes it a copy of the receiver instead, and a val is accepted. This suits a handle, a type whose fields share their storage, since writing through a copy changes the same storage the original refers to:

import sysl.buf.{Buf, buf}

trait Bump
    bump(*self)

struct Tally
    b: Buf[int]

impl Bump for Tally
    bump(self) = self.b.push(1)

val t = Tally(buf())

t.bump()
t.bump()

print(t.b.len())
2

That is how b[i] = x works on a val holding a Buf: IndexSet declares index_set(*self, …) and the Buf‘s implementation takes self. A generic body is checked again at each instantiation, so a call there uses the receiver of the implementation it reaches; a table slot for such a member points at an adapter that loads the value. Every other receiver must match, including the converse: an implementation that wants an address where the trait declares only a value would require something callers never agreed to provide:

trait Show
    show(self)

struct P
    v: int

impl Show for P
    show(*self) = print(self.v)
method 'show' of 'impl Show for P' takes a different receiver than the trait declares

Self is the implementing type, written wherever a signature needs to name it. Inside a generic impl it is the subject applied to that block’s parameters, so conformance treats -> Self and -> Box[T] as the same signature.

A trait that declares nothing is closed by its end marker, as a struct with no fields is. The emptiness is stated rather than inferred from a missing body, so a misindented member list cannot silently produce an empty trait. Such a trait is a name a bound can require, and an impl of it has nothing to supply:

trait Marked
end Marked

tagged[T: Marked](x: T) -> T = x

impl Marked for int

print(tagged(4))
4

Without the marker a body is still expected (indent expected). The library’s AnyBits is such a trait, and the compiler decides its membership.

A default may assume exactly what its own trait declares

A default’s body must serve every implementing type, and the trait is all they have in common. So a default’s body is checked once, at the trait, as the generic function it is: one type parameter, Self, bounded by the trait. A default calling a member the trait does not declare is reported at the trait, on its own line, even when nothing implements the trait.

It may not read a field of its receiver either. A bound describes behaviour and a field is layout, so no bound can allow one; see generics.

Every name in a default is resolved in the trait’s module, in its signature as well as its body. A default returning Buf[int], or taking a struct the trait’s module declares, is inherited by an impl in a module that imports neither, including one in another package. A type of the same name in the implementing module never substitutes for one the trait’s module cannot see; that is reported once, at the trait. Self is the one name a copy takes from the impl.

That holds for a default’s own type parameters too. A trait’s author cannot know what an implementing type calls its parameters, so a default declaring [U] and a type declaring [U] are two parameters that never meet: the receiver fixes the type’s, and the call solves the default’s.

trait Pick
    pick[U](self, u: U) -> U = u

struct Box[U]
    v: U

impl[U] Pick for Box[U]

print(Box(1).pick("x"))
x

A member written in the type’s own block has both lists in front of its author, and there a shared name is refused.

Beyond convenience, defaults let a trait grow: adding a member with a default does not break existing implementations, so a library can extend a trait after its first release.

A trait whose every member has a default leaves nothing to write, so the block body is empty, and writing the impl is the opt-in:

trait Retry
    retries(self) -> int = 3

struct Link
    n: int

impl Retry for Link

var l = Link(1)

print(l.retries())
3

The body a program runs is a copy per implementing type, generated under that type’s own name. This is monomorphization with Self as the parameter, so everything downstream (an ordinary call, a table slot, the escape summary) finds an ordinary function and needs no knowledge of its origin.

Replacing a default says override

An implementation may write the member itself instead of taking the trait’s body, and when it does it says so. Replacing a default is the same act as replacing an implementation, and takes the same keyword:

trait Retry
    retries(self) -> int = 3

struct Link
    n: int

impl Retry for Link
    override retries(self) -> int = self.n

var l = Link(4)

print(l.retries())
4

Without the keyword the member is refused. A reader of an impl block needs to know which members replace a default and which supply what the trait requires, and would otherwise have to open the trait to find out:

trait Retry
    retries(self) -> int = 3

struct Link
    n: int

impl Retry for Link
    retries(self) -> int = self.n

print(1)
trait 'Retry' supplies a body for method 'retries', so writing one here replaces it — say 'override retries', or leave the member out to keep the trait's

The keyword is also refused where nothing is replaced. A member that answers a bare requirement, as most members of most impl blocks do, supplies what the trait requires rather than replacing a body, so the keyword would be false:

trait Sized
    size -> int

struct Box
    w: int
    h: int

impl Sized for Box
    override size -> int = self.w * self.h

print(1)
trait 'Sized' declares property 'size' without a body, so this member supplies what the trait asked for rather than replacing anything — 'override' says a body was replaced

The rule rarely needs writing, because an implementation that keeps a default writes no member for it, and replacing a default is uncommon.

Conformance is explicit, always

A type that happens to have a member of the right name and shape does not satisfy a trait. There is no structural conformance; the impl documents the intent:

trait Named
    label(self) -> string

struct P
    n: string

    label(self) -> string = self.n

announce[T: Named](x: T) -> string = x.label()

print(announce(P("ada")))
'announce' requires its type parameter 'T' to implement 'Named', but P does not

Swift protocols, Kotlin interfaces and Scala traits are nominal too. Nominal conformance rules out accidental conformance, where a type satisfies a trait only because two names happen to match.

Retrofitting is still possible. You may impl your trait for a type you do not own; you do it explicitly rather than by structural match.

Reaching a member through its trait

A member that an impl gives a type is reached on a value of that type without importing the trait. The impl travels with the type: wherever the program can see it (and a program sees every impl of the modules it is built from), x.member() finds it, as Scala 3 finds a given through its implicit scope. So a float has its sqrt and a slice its map in a file that imports nothing:

val a = [1, 2, 3]
val xs = a[0..]
val r = 2.25

print(xs.map(x -> x * 2), r.sqrt(), (-r).abs(), real.epsilon() < 1.0e-15)
[2, 4, 6] 1.5 2.25 true

The traits the file names are searched first. The lookup has two tiers. The first is the traits the file can name: its own module’s, the ones it imported, and the ones a bound in force provides. Only where none of those gives the type a member of that name does the second tier apply: every trait whose impl the program can see. So a program’s own trait takes precedence over a library trait with the same member name that the file never mentions, and an import still settles which of two traits is meant:

trait Level
    magnitude(self) -> Self

impl Level for f32
    magnitude(self) -> f32 = self + 1

print(f32(-2).magnitude())
-1

sysl.math.Magnitude also gives f32 a magnitude; this file names Level and not Magnitude, so Level‘s member is the one called.

Where the file names none of several traits that each give the type the member, the call is ambiguous. With a Weigh in one module and a Measure in another, each implemented for a Box from a third, a file importing only Box is refused at Box(n).size() with

'size' on boxes.Box comes from measure.Measure and weigh.Weigh, and this file names none of them — nothing in the call says which was meant; import the one that was meant

and importing either trait resolves it. A trait private to its module is not reached from outside that module by either tier.

Generic code still needs the bound. Inside f[T](x: T) the type is not known, so no impl can be found for it: a T has the members its bounds provide and no others, and x.sqrt() there requires T: Float.

A bound on a family carries what a block over that family implements. impl[T: Integer] Twice for T says every integer implements Twice, so a [T: Integer] body has its members and can hand its T to something asking T: Twice; nothing about the instantiation can fall outside the block.

trait Twice
    twice(self) -> Self

impl[T: Integer + Add] Twice for T
    twice(self) -> T = self + self

doubled[U: Twice](x: U) -> U = x.twice()

both[T: Integer + Add](x: T) -> (T, T) = (x.twice(), doubled(x))

print(both(u8(21)))
(42, 42)

The lookup is the same two tiers, read at the parameter. A bound written on the parameter answers first, so [T: Integer + Mine] means Mine‘s member of the name. Then come the blocks over its family whose traits the file names, then those it can only see, and two at one tier are refused rather than picked between:

'top' on T comes from A and B, and each is in scope here — nothing in the call says which was meant

A block asking more than the family, impl[T: Integer + Zed] Cond for T, is reached only by a parameter whose own bounds promise the rest.

Two traits may declare a member of one name

The two members do not conflict, and a type may implement both traits. The type the call goes through decides which is meant, so erasing to one trait or the other selects it:

trait Weigh
    size(self) -> int

trait Measure
    size(self) -> int

struct Box
end Box

impl Weigh for Box
    size(self) -> int = 1

impl Measure for Box
    size(self) -> int = 2

val w: &Weigh = Box()
val m: &Measure = Box()

print(w.size(), m.size())
1 2

Called on the value itself, with both traits in scope, the call is ambiguous:

trait Weigh
    size(self) -> int

trait Measure
    size(self) -> int

struct Box
end Box

impl Weigh for Box
    size(self) -> int = 1

impl Measure for Box
    size(self) -> int = 2

print(Box().size())
'size' on Box comes from Weigh and Measure, and each is in scope here — nothing in the call says which was meant

The refusal depends on each is in scope here. A file importing only one of the two has no ambiguity to resolve, which is the ordinary case, so this is rarely encountered.

Sharing one name has no other cost. The trait’s other members remain the type’s, and a bound reaches every one of them, including an associated function called through the type’s name. Here Level shares magnitude with the library’s sysl.math.Magnitude, which f32 already implements:

trait Level
    magnitude(self) -> Self
    half() -> Self

impl Level for f32
    magnitude(self) -> f32 = self * 2
    half() -> f32 = 0.5

h[S: Level]() -> S = S.half()
twice[S: Level](x: S) -> S = x.magnitude()

print(h[f32](), f32.half(), twice(f32(-3)))
0.5 0.5 -6

The same rule holds through the type’s name as on a value: an associated function both traits declare, called as f32.half() with both in scope, is refused with the message above, and a bound naming one of the two settles it.

The ambiguity cannot be avoided for a member with no arguments on a type that implements both traits, so a member that two traits both need belongs in a required trait rather than in each. Writer and Reader both need to report whether a stream has failed, and an open file is both; a failed declared by each would produce the refusal above on exactly the type that most needs it. The library instead has both require Fallible, which removes the ambiguity.

The two dispatch strategies

A trait can be used in two ways, and choosing between them is the main decision whenever polymorphism is needed.

[T: Trait] — static&Trait / *Trait — dynamic
what happensone specialized copy per concrete Tone copy of the code, dispatched through a table
the calldirect, inlinableindirect, not inlined
the valueby value, no indirectionbehind a fat pointer, always
the set of typesfixed at compile timeopen
costcode sizeone indirect call
use it whenthe type is known, which is most of the timethe collection is heterogeneous, or the boundary is a plugin

Static is the default:

trait Named
    label(self) -> string

struct Pump
    tag: string

struct Valve
    tag: string

impl Named for Pump
    label(self) -> string = "pump " + self.tag

impl Named for Valve
    label(self) -> string = "valve " + self.tag

announce[T: Named](x: T) -> string = x.label()

print(announce(Pump("P-1")), announce(Valve("V-7")))
pump P-1 valve V-7

Dynamic dispatch is for values whose types vary at run time:

trait Shape
    area(self) -> int

struct Rect
    w: int
    h: int

struct Square
    s: int

impl Shape for Rect
    area(self) -> int = self.w * self.h

impl Shape for Square
    area(self) -> int = self.s * self.s

var shapes: [2]&Shape = [Rect(2, 3), Square(4)]

print(shapes[0].area(), shapes[1].area())
6 16

The same trait supports both strategies, chosen by how the parameter is written: a bound for static dispatch, a trait object behind a sigil for dynamic. There is no dyn keyword, because the sigil already says it.

The two work together. A trait object satisfies a bound on the trait it dispatches through, so announce above accepts a &Named as readily as a Pump; the table the object carries holds exactly the members the bound names:

trait Named
    label(self) -> string

struct Pump
    tag: string

impl Named for Pump
    label(self) -> string = "pump " + self.tag

announce[T: Named](x: T) -> string = x.label()

var d: &Named = Pump("P-1")

print(announce(Pump("P-1")), announce(d))
pump P-1 pump P-1

The table above therefore describes what the caller holds, not which functions it can reach. A library written against bounds also serves a program that uses trait objects, with no second signature. See generics for why: an object type is a concrete type, so monomorphization applies to it unchanged.

Any type may carry an impl

impl Show for int is as ordinary as impl Show for Point, and so is impl Show for []int. This is essential rather than convenient: a library cannot be written against a trait that cannot cover int, and the library’s own Display depends on it.

trait Double
    twice(self) -> int

impl Double for int
    twice(self) -> int = self * 2

print(5.twice(), (2 + 3).twice())
10 10

Every type has one owner key under which its members are recorded: a struct or an enum by its declared name, every other type by its canonical name. So impl Show for int and impl Show for i32 are one implementation, not two, and writing both is reported as a duplicate.

An impl‘s subject is a type reference, not an identifier, so types with no name of their own carry an implementation exactly as named ones do:

trait Total
    total(self) -> int

impl Total for [3]int
    total(self) -> int = self[0] + self[1] + self[2]

var a = [1, 2, 3]

print(a.total())
6

An array’s length is part of its type, so [2]int and [3]int are two types and may implement the same trait differently.

Two subjects are refused, because an implementation for either would have no effect:

  • a memory mode, such as *Point or &Point. A mode is a way of holding a Point rather than a separate type, and a member call already sees through one level of * or & to find the receiver’s members, so an impl for the mode would register members nothing could reach;
  • a trait object, such as *Show. An impl says how one particular type behaves, and an erased value does not know which type it holds.
struct P
    v: int

trait Show2
    show2(self) -> int

impl Show2 for *P
    show2(self) -> int = 1

print(1)
'*P' is a way of holding a P rather than a type of its own — write the 'impl' for P

A compiler-provided member cannot be implemented, for the same reason a field cannot. len on a slice or an array, and bytes on a string, are found before the member table is consulted, so an impl declaring one would register a member no call could reach.

Where an impl may live

An impl is unnamed (nothing at a use site says which one to apply), so resolving T: Show or 5.show() means searching for an implementation. In a program of several modules, that search must be bounded; otherwise it would range over every module in the program, which would make separate compilation impossible.

An impl Trait[A…] for Type may appear only in the module that declares Trait, or in one that declares a type the block names, in Type or among A…. Resolving a bound therefore inspects only modules the use site already depends on in order to name the trait and the type. There is no global search, and no dependency the source does not show.

This is Rust’s orphan rule, and it leaves the common cases open:

  • retrofitting works: impl MyTrait for TheirType lives with MyTrait, and the trait’s module makes it valid;
  • impl Show for int works: a built-in type has no module of its own, so its owner key belongs to the library, and every impl on one is made valid by its trait’s module instead;
  • a composed type belongs to a module when anything named in it does: override impl Display for []Point is valid in the module that declares Point, while a block for []int names nothing outside the library and has no home. (It says override because the library implements Display for every slice. The two rules are separate, and a slice of your own struct needs both: coherence to give the block a home, and override to take precedence over the block already covering it);
  • a type parameter is not a local type, so impl[T: Display] Display for []T is refused however its bound is written. Making every printable slice printable is the library’s job;
  • a local type in the trait’s arguments also gives the block a home, which is how a scalar can appear on the left of an operator. impl Mul[Point, Point] for real is written in the module that declares Point: real and Mul both belong to the library, and the argument list is what places the block in that module:
struct Point
    x: real
    y: real

impl Mul[real] for Point
    mul(self, k: real) -> Point = Point(self.x * k, self.y * k)

impl Mul[Point, Point] for real
    mul(self, p: Point) -> Point = Point(p.x * self, p.y * self)

val p = Point(1, 2)
val a = p * 2
val b = 2 * p

print(a.x, a.y, b.x, b.y)
2 4 2 4

The rule forbids the case with no home: a foreign trait implemented for a foreign type, where two unrelated modules could each supply a different implementation and no rule could pick one. So impl Mul[real, real] for real is refused in any module but the library’s, since it names nothing of its own in either place:

impl Mul[real, real] for real
    mul(self, k: real) -> real = self

print(1)
an 'impl' may be written only in the module that declares the trait or in one that declares a type the block names, and 'Mul' is the library's while nothing this block names is declared outside the library — so this one has no home

The built-in implementation is unaffected. real implements Mul regardless, and only the argument list tells the two apart, so 2.0 * 3.0 remains the machine’s multiply, and the block above applies to real only for the argument pair it was written for.

An impl is part of its module’s public surface. Adding, removing, or changing one is an interface change visible to everything downstream — the same reasoning that puts implicit-resolution schemes out of scope. See modules.

An impl covers a generic type as a whole

A block may declare type parameters of its own, written directly after the keyword:

struct Box[T]
    v: T

trait Show
    show(self) -> string

impl[T: Display] Show for Box[T]
    show(self) -> string = "box of " + self.v.to_string()

var b = Box(41)

print(b.show())
box of 41

That is one implementation for every Box, and its members are monomorphized per receiver exactly as a generic type’s own members are.

Its subject must be the type applied to the block’s parameters and nothing else: each argument a single parameter, each parameter used once, and every parameter used:

struct Box[T]
    v: T

trait Show2
    show2(self) -> int

impl Show2 for Box[int]
    show2(self) -> int = 1

print(1)
'Box' is generic, so an 'impl' for it covers every instantiation at once — write 'impl[T] Show2 for Box[T]'

A generic type has one key for all of its instantiations, so an implementation for some of them would be a second implementation under a key that already holds one. Overlapping implementations, and the specialization rule needed to choose between them, are deliberately left out of the language.

The parameters are matched to the arguments by position in the subject, not by declaration order, so impl[X, Y] Show for Pair[Y, X] means what it appears to.

Conditional conformance

A bound on the block makes the conformance conditional. In the Box example above, a Box[T] implements Show exactly when T implements Display, so Box[int] does and a Box of something that cannot be displayed does not.

The condition is checked one level in, and it composes: under impl[T: Show] Show for Box[T], a Box[Box[int]] conforms exactly when Box[int] does, which makes a conditional implementation usable on nested types.

struct Box[T]
    v: T

trait Show
    show(self) -> string

impl Show for int
    show(self) -> string = "i"

impl[T: Show] Show for Box[T]
    show(self) -> string = "b" + self.v.show()

var b = Box(Box(1))

print(b.show())
bbi

Every check of conformance works the same way: a generic function’s bound, an erasure to a trait object, print looking for a Display, a call of the member itself. An instantiation that fails the condition is refused at each of them, while other instantiations are not:

struct Box[T]
    v: T

trait Show
    show(self) -> string

trait Mark
    m(self) -> int

impl[T: Mark] Show for Box[T]
    show(self) -> string = "b"

struct N
    n: int

print(Box(N(1)).show())
'show' is a member the 'impl' of 'Show' gives 'Box[N]' only where its condition holds, and it asks 'Mark' of N, which does not implement it

Beyond deciding conformance, the bounds make the members checkable at their definition: a block states what it assumes, so its bodies are checked once against those bounds alone, and a method calling something no bound allows is reported on its own line before anything is instantiated.

A shape is covered the same way

A composed type has no name to be generic over, but it has a shape, and a block with type parameters may match the shape instead:

trait Count
    count(self) -> usize

impl[T] Count for []T
    count(self) -> usize = self.len

var xs: []int = [1, 2, 3, 4]

print(xs.count())
4

The rules above apply unchanged: the subject is the shape applied to the block’s parameters and nothing else, so impl[T] Count for [][]T is refused because the element is a shape rather than a parameter, and so is a fixed element type:

trait Count
    count(self) -> int

impl[T] Count for []int
    count(self) -> int = 1

print(1)
'int' fixes the element type, and an 'impl' with type parameters covers every slice — write one of the block's own parameters here

Two rules are specific to shapes. A composed type is recorded under its whole self ([]int, not []), so a shape needs a key of its own, formed by dropping the arguments; a lookup that finds nothing under the type’s own key falls back to the shape’s. And a shape with a type parameter keeps the array’s length written out: [2]T and [3]T are two shapes, each covering every element type at its own length. (A block covering every length takes a value parameter, [N]T, as described on generics.) A shape whose length is a value parameter may keep its element written out instead: [N]u8 is every array of u8 and <N>f32 every vector of f32, beside [N]T and <N>T; generics has the array’s rules and vectors the vector’s.

A string is not covered by []T. It is a view of bytes that are valid UTF-8, and that invariant is what distinguishes it from a []u8; a block written for every slice says nothing about it. "hi".bytes is a []const u8 and is covered, as the next rule explains.

Both views of a slice are one shape

[]T and []const T are one type with a flag rather than two types (arrays.md), and they share one shape. A block written for either subject therefore reaches a receiver of either view:

trait Count
    count(self) -> usize

impl[T] Count for []T
    count(self) -> usize = self.len

var xs = [1, 2, 3]
val view: []const int = xs[..]

print(view.count(), "hi".bytes.count())
3 2

The block is written against what a slice is, a pointer and a count of T, and whether a given slice may be written through is not part of that.

The covering does not give a member a permission its receiver lacks. The block is instantiated once per view, so the instance a read-only receiver reaches has a self it may not write through. A member that writes is refused, in its own body:

trait Bump
    bump(self)

impl[T] Bump for []T
    bump(self)
        self[0] = self[0]

var xs = [1, 2, 3]
val view: []const int = xs[..]

view.bump()
made real at the read-only view because that is what the receiver was

The refusal is reported on the line that writes rather than on the call, where a reader can act on it. Because the block’s own file wrote []T and never wrote const, the message also names the receiver that selected this instance. bump remains callable on a []int:

trait Bump
    bump(self)

impl[T] Bump for []T
    bump(self)
        self[0] = 9

var xs = [1, 2, 3]

xs[..].bump()

print(xs[0])
9

Write []const T as the subject where the members only read. Nothing requires it, and both forms reach both views, but the subject is where a reader looks to see what a block does with its receiver, and stating it there saves discovering it from a refusal.

A shape and a written-out type overlap, and an unmarked overlap is refused. Both blocks would define the member for a []int, so whichever is written second is refused, and the diagnostic names the one already there:

trait Show2
    show2(self) -> int

impl Show2 for []int
    show2(self) -> int = 1

impl[T] Show2 for []T
    show2(self) -> int = 2

print(1)
'[]int' already implements 'Show2', and this 'impl' would implement it for every slice — including that one

Refusing is the right default: two overlapping blocks are usually a mistake, such as an accidental duplicate or a block placed in the wrong module, and the refusal finds it.

override — when the overlap is deliberate

An implementation may be marked override, and then it takes precedence:

trait Show2
    show2(self) -> int

impl[T] Show2 for []T
    show2(self) -> int = 2

override impl Show2 for []int
    show2(self) -> int = 1

var xs: []int = [7, 8]
var ss: []string = ["a"]

print(xs.show2(), ss.show2())
1 2

The keyword goes on the overriding side, not the overridden one. This is the opposite of C#’s virtual/override pair and of Rust’s unstable default, both of which make the general implementation grant permission in advance. A library author cannot know which implementations someone will need to replace; the intent belongs to the writer of the override, not the writer of the original.

The keyword grants no permission; what it provides is the diagnostic. An unmarked second implementation is still refused exactly as above.

The overriding side is always a type written out in full. A shape is one key and so is a generic type’s name, and the blocks that would sit under those are already refused: impl[T] Show2 for [][]T matches a shape’s argument by its shape, and impl Show2 for Box[int] fixes one instantiation of a type whose blocks cover every instantiation. There is nothing more general for either of them to replace, and marking one override is refused with that explanation:

trait Show2
    show2(self) -> int

override impl[T] Show2 for []T
    show2(self) -> int = 2

print(1)
'override' says this block replaces a more general one, and '[]T' is the general kind — an implementation for a shape or for a generic type covers every type it matches at once, so there is nothing below it. The override is written on the block for one type spelled out in full

An override that overrides nothing is refused. This is the check in the other direction, and it matters when a library removes or narrows an implementation a program overrides: without it, the override would silently become the only implementation while still claiming to replace one.

trait Show2
    show2(self) -> int

override impl Show2 for []int
    show2(self) -> int = 1

print(1)
'[]int' says 'override', but nothing else implements 'Show2' for it — an override replaces an implementation that covers the type more generally, and there is none to replace

Coherence, not the keyword, keeps this sound. The usual hazard of overriding is two method tables for one type, such as a []Point erased to a *Show2 using one implementation at one site and the other elsewhere. An impl may live only in the module declaring the trait or in one declaring a type named in the subject, so a program cannot write impl[T] Display for []T at all, because []T names no type of its own. The only override that can be written across a module boundary is one that names the writer’s own type, so there is at most one per type and it lives with that type, and any site that can write []Point already depends on the module declaring Point. One type has one table.

The remaining cost is that a library cannot rely on its own implementations being the ones in use. override makes every such site searchable rather than invisible.

The compiler writes four of them

Four traits follow directly from structure. Eq on a product compares its fields one by one; Ord makes those comparisons in declaration order; Hash mixes the fields; Display writes the name and the fields. Writing them by hand is mechanical and fails quietly: a field added to a struct and forgotten in its eq is silently left out of the comparison.

A derives clause on a struct or an enum asks for them:

struct Size derives Eq, Ord, Hash, Display
    w: int
    h: int

val a = Size(3, 4)

print(a == Size(3, 4), a < Size(3, 5), a)
true true Size(3, 4)

The clause goes after the name and its type parameters, before the body. It produces an ordinary impl block, the one a person would have written, so a derived implementation is found, checked, dispatched and erased exactly as a written one is, and a field that does not support the trait is reported in the ordinary way.

Ord is the total order and PartialOrd the one it extends (reference/expressions.md § Operator dispatch). derives Ord derives PartialOrd — whose lt and le the comparisons call — and gives Ord the empty block that promises the order is total, so it refuses a field whose type is only partially ordered, a float above all, naming it. derives PartialOrd is the clause for such a type, and its comparisons are all false against a NaN field, as Rust’s derived PartialOrd gives:

struct Reading derives PartialOrd
    at: int
    value: real

val hole = Reading(1, 0.0 / 0.0)

print(Reading(1, 2.0) < Reading(1, 3.0), hole <= Reading(1, 2.0), hole >= Reading(1, 2.0))
true false false
struct Reading derives Ord
    at: int
    value: real

print(1)
'Reading' cannot derive Ord: its field 'value' is real, which is only 'PartialOrd', so two of its values may be neither less, greater nor equal. Derive 'PartialOrd' instead

Each of the four is written by the library, as the trait’s own derive member: the same mechanism a program’s own trait uses, so the compiler treats the four specially only in what the clause refuses up front. The derived Display writes the name and each part directly to the destination; where a width is requested, it first renders into a Counting sink to learn the width, pads once, and then renders for real. A derived rendering therefore builds no string, and an ordinary print of one takes a single pass. A shape with no parts, such as a fieldless struct or an enum whose variants carry nothing, renders as its name.

The library provides the same four for every tuple, and a derived block walks a named product by the same rules as a tuple, so a Size and an (int, int) compare and order alike, and render alike except for the name in front. Any other trait is derived by its own derive members, described below.

A reference is compared and hashed through its referent

A &T meets Eq and Hash for every T: as T does wherever T has them, and by its identity where it does not (References compare through their referent). Wherever a bound asks for either — a [K: Hash + Eq] body, a tuple’s parts, a derived block’s fields — a reference answers as == on two of them does, so the hash always agrees with the equality:

struct Node derives Hash
    level: int

impl Eq for Node
    eq(self, other: Node) -> bool = self.level == other.level

struct Cell
    level: int

struct Edge derives Eq, Hash
    from: &Node
    to: &Cell

same[T: Eq](a: T, b: T) -> bool = a.eq(b)

main()
    val a: &Node = Node(1)
    val b: &Node = Node(1)
    val c: &Cell = Cell(1)
    val d: &Cell = Cell(1)

    print(same(a, b), same(c, d), same(c, c))
    print(Edge(a, c) == Edge(b, c), Edge(a, c) == Edge(a, d), Edge(a, c).hash() == Edge(b, c).hash())
true false true
true false true

Cell has neither trait and Edge derives both all the same: a reference is a key whatever it names, by value where the referent has one and by identity where it has not. A referent with an Eq and no Hash is the one refused: its references compare by value, and the address’s hash would not agree. === is identity whatever the referent defines — on a trait object too, where it is the object’s (below) — and ByAddress keys a table by it. A *T meets both by its address, and a trait requiring either is refused through one. A reference to a trait object is two words and meets Hash only through its table.

A trait says how it is derived

A trait may carry a derive member: derive[T] name(self: T, …), with a body written over the structure of T using reflection: T::Kind, for const f in T::Fields, self.(f). (A member with no receiver is derived with no self, below.) A derives clause naming the trait instantiates each derive member with T standing for the type, producing the impl block a person would have written:

@annotation
struct rename
    name: string

trait Json
    encode(self, w: *Writer)

    derive[T] encode(self: T, w: *Writer)
        if const T::Kind == Kind.Struct
            w.write("{".bytes)

            for const f in T::Fields
                if f.index > 0 then w.write(",".bytes)

                val key = f::Annotation[rename].map(r -> r.name).unwrap_or(f.name)

                w.write(s"\"$key\":".bytes)
                self.(f).encode(w)

            w.write("}".bytes)
        else
            @assert(false, "Json derives for a struct")

impl Json for int
    encode(self, w: *Writer) = w.write(s"$self".bytes)

impl Json for string
    encode(self, w: *Writer) = w.write(s"\"$self\"".bytes)

struct User derives Json
    @rename("id") user_id: int
    name: string

User(7, "ann").encode(stdout())
print("")
{"id":7,"name":"ann"}

User gets an impl Json for User whose body is two unrolled field writes. The body is resolved in the trait’s file, under the trait’s imports, and it is a template rather than a default: a block written by hand inherits nothing from it. A generic type derives at each instance (struct Box[T] derives Json requires T: Json of each), and a data enum is walked with for const c in T::Variants.

The copy is checked once per field, and an error is reported at the derives: a field whose type does not support what the body asks is reported against the deriving type, where it requested the derivation, naming the field. A call in the body to a member the trait declares means that trait’s member, as a bound T: Json would: a field whose type has a member of the same name from another trait is still refused as not implementing Json, and is never passed to the other trait’s member. A member of a trait the derived one requires is reached the same way, through the trait that declares it.

trait Json
    to_json(self) -> string

    derive[T] to_json(self: T) -> string
        var out = ""

        for const f in T::Fields
            out = out + self.(f).to_json()

        out

impl Json for int
    to_json(self) -> string = s"$self"

struct Pin
    on: bool

struct Lamp derives Json
    level: int
    pin: Pin

print(Lamp(1, Pin(true)).to_json())
'Pin' does not implement 'Json': method 'to_json' is missing (in the copy for field 'Lamp.pin')

A derivation reaches the type’s private fields. self.(f) on a private field is otherwise refused outside the type’s own file; naming the trait in derives is the type author’s opt-in.

A trait with no derive member — and none among the traits it requires (§ “A trait that joins others derives what it requires”) — cannot be derived, and is implemented with an impl block:

trait Show
    show(self) -> string

struct P derives Show
    x: int
'Show' is not a trait the compiler knows how to write, and it carries no 'derive' member

A derived member may have type parameters of its own

The bracket after derive names the deriving type and nothing else. A member that is generic in the trait is generic in its derive member too, and its own type parameters follow its name, as they do in the trait: derive[T] encode[E: Encoder](self: T, e: *E). Each deriving type gets one body, instantiated per (T, E) at each use, so one derivation serves every format with a direct call to each of the format’s members:

trait Sink
    put(*self, s: string)

trait Show
    show[S: Sink](self, s: *S)

    derive[T] show[S: Sink](self: T, s: *S)
        s.put(T::BaseName)

        for const f in T::Fields
            s.put(s" ${f.name}=")
            self.(f).show(s)

impl Show for int
    show[S: Sink](self, s: *S) = s.put(s"$self")

struct Plain
    out: string

impl Sink for Plain
    put(*self, s: string) = self.out = self.out + s

struct Bracketed
    out: string

impl Sink for Bracketed
    put(*self, s: string) = self.out = self.out + s"[$s]"

struct Pt derives Show
    x: int
    y: int

var p = Plain("")
var b = Bracketed("")
Pt(1, 2).show(&p)
Pt(1, 2).show(&b)
print(p.out)
print(b.out)
Pt x=1 y=2
[Pt][ x=][1][ y=][2]

The derived member is the trait’s member written once for every type, so its type parameters are held to the trait’s: as many, in the same order, each with the bounds the trait declares for it, in the bracket or in a where. They may be renamed, and may not reuse the deriving type’s name. A missing bound is refused, since the body could not ask for what the trait promises every caller, and so is an extra one, which would ask a caller for what the trait never required:

trait Sink
    put(*self, s: string)

trait Show
    show[S: Sink](self, s: *S)

    derive[T] show[S](self: T, s: *S) = s.put(T::BaseName)

print(1)
'derive' member 'show' leaves 'S' without the bound 'Sink' trait 'Show' declares for its 'show' — write 'S: Sink'

Written inside the derive bracket, the member’s own parameters are refused with where they go:

trait Sink
    put(*self, s: string)

trait Show
    show[S: Sink](self, s: *S)

    derive[T, S: Sink] show(self: T, s: *S) = s.put(T::BaseName)

print(1)
a member's own type parameters follow its name, as in 'derive[T] show[S: Sink](self: T, …)'

A derived member may have no receiver

A trait member with no receiver — an associated function, such as decode(s: *S) -> Self — is derived by a derive member with no self: its parameter list holds the member’s own parameters and nothing else, and T stands where the member says Self, typically in the result. Each deriving type gets an associated function of its own, called on the type, P.decode(&s), or through a bound, T.decode(s). A decoder is the case it exists for: there is no value to start from, so the body builds one field by field:

trait Source
    next(*self) -> int

trait Decode
    decode[S: Source](s: *S) -> Self

    derive[T] decode[S: Source](s: *S) -> T
        var out: T

        for const f in T::Fields
            out.(f) = field[f::Type, S](s)

        out

field[U: Decode, S: Source](s: *S) -> U = U.decode(s)

impl Decode for int
    decode[S: Source](s: *S) -> int = s.next()

struct Count
    n: int

impl Source for Count
    next(*self) -> int
        self.n += 1
        self.n

struct Inner derives Decode
    z: int

struct Pt derives Decode
    x: int
    inner: Inner
    y: int

var c = Count(0)
val p = Pt.decode(&c)
print(p.x, p.inner.z, p.y)
1 2 3

Whether a derive member takes a receiver is decided by the member it writes, so one with self for a member without, or the other way about, is refused in the trait:

trait Make
    make() -> Self

    derive[T] make(self: T) -> T = self

print(1)
'derive' member 'make' takes a receiver, but the 'make' trait 'Make' declares takes none — a member with no receiver is derived with no 'self', as 'derive[T] make(…)'

Every derive member’s signature is its member’s, parameter by parameter and in its result, with T for Self and its own type parameters for the member’s. It is compared where the trait is declared, whether or not anything derives it, so a mistake is told once, in the trait:

trait Make
    make() -> Self

    derive[T] make() -> int = 0

print(1)
'derive' member 'make' returns int, but trait 'Make' declares T — a 'derive' member writes that member's body, so it takes its signature, with 'T' for 'Self'

A body with no receiver has no self to read; naming one is refused at the derives that asked for the copy, as any mistake in a derived body is.

A trait that joins others derives what it requires

A trait with no derive member of its own may still be named in derives when what it requires does carry them: trait Codec: Encode + Decode asks for both, so derives Codec derives Encode and Decode and gives Codec the empty impl a reader would have written. Requirements are followed through every trait that only joins others, and each trait is derived once however many paths reach it:

trait Encode
    encode(self) -> string

    derive[T] encode(self: T) -> string
        var out = T::BaseName

        for const f in T::Fields
            out = out + " " + self.(f).encode()

        out

impl Encode for int
    encode(self) -> string = s"$self"

trait Decode
    decode(n: *int) -> Self

    derive[T] decode(n: *int) -> T
        var out: T

        for const f in T::Fields
            out.(f) = field[f::Type](n)

        out

field[U: Decode](n: *int) -> U = U.decode(n)

impl Decode for int
    decode(n: *int) -> int
        *n += 1
        *n

trait Codec: Encode + Decode

struct Pt derives Codec
    x: int
    y: int

struct Box[T] derives Codec
    v: T

round[T: Codec](v: T, n: *int) -> string = v.encode() + " /" + T.decode(n).encode()

var n = 0
print(round(Pt(5, 6), &n))
print(Box(Pt(7, 8)).encode(), Box[int].decode(&n).v)
Pt 5 6 /Pt 1 2
Box Pt 7 8 3

A generic type’s parameters are bound by each trait’s own block — Box[int] is Encode and Decode though int is never Codec. A required trait that reaches no derive member is the type’s to implement, and the supertrait check names it (Debug is never asked for: every type has one). A joining trait that reaches none at all is refused as any trait without a derive member is.

A type has one implementation of a trait, so a trait the joining one would derive and the type also implements by hand is refused at the clause, which names what is left to derive:

trait Encode
    encode(self) -> string

    derive[T] encode(self: T) -> string = T::BaseName

trait Decode
    decode() -> Self

    derive[T] decode() -> T
        var out: T

        out

trait Codec: Encode + Decode

struct H derives Codec
    x: int

impl Encode for H
    encode(self) -> string = "by hand"

print(1)
'Codec' would derive 'Encode' for H, which already has an 'impl Encode' written by hand — a type has one implementation of a trait. To derive the rest, name the others instead ('derives Decode') and keep the 'impl'

A trait the clause names outright and also reaches through a joining one is refused at its name, as a trait named twice is — derives Codec, Encode is told to drop Encode.

Order is declaration order

Ord is lexicographic, first field first: the only ordering a named product can claim, and the one a tuple has. That makes the order of the fields part of the type’s contract, as it already is for layout, since a struct’s fields are laid out in the order they are written.

struct Version derives Ord, Display
    major: int
    minor: int

print(Version(1, 9) < Version(2, 0), Version(2, 0) < Version(1, 9))
true false

A type that orders by a particular field should implement Ord by hand rather than derive it. That is the case the clause does not cover.

A generic type derives conditionally

Every type parameter gains the derived trait as a bound, which is conditional conformance written for you: a Box[int] is Eq and a Box of something without Eq is not, with nothing further to write.

struct Box[T] derives Eq, Display
    v: T

print(Box(1) == Box(1), Box(1) == Box(2), Box("x"))
true false Box(x)

A const value parameter gains no bound, since it is not a type, and the type’s own bounds are kept, since struct Sorted[T: Ord] is only a valid type where T is Ord.

derives Ord writes two blocks, and each is conditional on its own trait: a Box[T] derives Ord is PartialOrd wherever T is and Ord wherever T is, so a Box[real] compares and a sort refuses it, exactly as a (real, int) does.

An enum takes the clause too

A variant renders under its own name, as it is written. Comparison is by variant first, in declaration order, and then field by field within the variant.

enum Shape derives Eq, Ord, Display
    Circle(r: int)
    Rect(w: int, h: int)
    Empty

print(Circle(2), Rect(3, 4), Empty)
print(Circle(9) < Rect(0, 0), Rect(1, 5) < Rect(1, 6), Circle(1) == Circle(1))
Circle(2) Rect(3, 4) Empty
true true true

The hash mixes the variant before the payload, so two variants carrying equal payloads hash differently instead of colliding in a table that holds both.

A simple enum, one where no variant carries a payload, is already Eq, because its value is its discriminant. The clause reports that directly rather than producing a block that is refused later:

enum Colour derives Eq
    Red
    Green
a simple enum is already 'Eq'

Its Ord is its discriminants’ order, so a simple enum still has three worth deriving:

enum Colour derives Ord, Hash, Display
    Red
    Green
    Blue

print(Red < Green, Blue < Green, Green)
true false Green

What the clause deliberately cannot say

It takes no override. Nothing in the library covers a type you declared, so there is no more general block for a derived one to take precedence over; where one is needed, an override impl is written by hand, and the two forms do not need to combine.

Derivation is all or nothing per trait. A derived block cannot have one of its methods replaced, and a derived block beside a hand-written one for the same trait is a duplicate implementation.

It also does not apply to a type whose fields it cannot see. An opaque struct with no body is C’s incomplete type: the storage belongs to whoever allocated it, and its shape is unknown here. A derived Eq over no fields would answer true for every pair without any basis:

opaque struct Handle derives Eq
opaque and declares no fields

This is the only case where visibility matters, and it concerns a layout that is absent rather than one that is hidden. The clause is part of the declaration, so the block it writes is in the module that declares the type: a private field is walked, because the block is written where the type is.

Every type has a Debug

Debug is the library’s other rendering trait, with the same shape as Display (a sink and the field the whole value occupies), and it is the one exception to conformance being explicit: every type has one, and none has to be written. The compiler supplies a Debug for every struct and enum that does not write its own, as a derives clause would but without being asked: the name, then each field by name, each shown through its own Debug, so a string inside a structure is quoted and escaped. An enum’s variant is shown under the enum’s name.

struct Point
    x: int
    y: int

enum Color
    Red
    Green

struct Label
    text: string
    at: Point
    color: Color

print(Label("a \"b\"", Point(1, 2), Red))
Label(text: "a \"b\"", at: Point(x: 1, y: 2), color: Color.Red)

The library writes the rest: the scalars (a char in single quotes, a string in double quotes), tuples, arrays, slices, Buf, Option and Result, each shown as its Display shows it but with the parts through their Debug. So print of a value with neither block written still prints: print, x.to_string() and an interpolation hole use Display where a value has one and Debug where it does not, and f"${x}%?" and x.debug_string() always use Debug (strings). assert_eq reports both sides through it, so got "1", want 1 cannot be mistaken for equal values.

An impl Debug of your own replaces the supplied one, for example to hide a field or abbreviate a large buffer, and a structure holding the type shows it through that block too:

struct Secret
    key: string

impl Debug for Secret
    debug(self, out: *Writer, fmt: FormatSpec) = display_pad("Secret(***)".bytes, out, fmt)

struct Login
    user: string
    secret: Secret

print(Login("ed", Secret("9f2a71c4")))
Login(user: "ed", secret: Secret(***))

A generic type’s supplied block is conditional, as a derived one is, and every type meets the condition, so a T: Debug bound places no burden on a caller:

show[T: Debug](x: T) = print(f"${x}%?")

struct Box[T]
    v: T

show(Box("in"))
show(3)
Box(v: "in")
3

A type’s own debug is unaffected by the supplied one. A field, a variant or a member named debug keeps the name on the type, and %?, x.debug_string() and a T: Debug bound still use the supplied rendering:

struct Options
    title: string
    debug: bool

impl Options
    describe(self) -> string = if self.debug then "verbose" else "quiet"

var o = Options("run", false)
o.debug = true
print(o.debug, o.describe())
print(f"${o}%?")
true verbose
Options(title: "run", debug: true)

An impl Debug written for such a type is refused, as is any trait member that shares a field’s name:

struct Flags
    debug: bool

impl Debug for Flags
    debug(self, out: *Writer, fmt: FormatSpec) = display_pad("Flags".bytes, out, fmt)
type 'Flags' has both a field and a member named 'debug'

What is shown by rule

A few values have no block that could be written for them, and are shown by a fixed rule:

  • a raw pointer *T shows its address, in lower-case hex with no padding, and the null pointer as 0x0. It is not followed, since what it points at may be gone. This difference is deliberate: a member call sees through one * (above), so in generic code a T: Debug instantiated at *P uses P‘s Debug (the bound is met by what the pointer points at), while print(p), f"${p}%?" and a field of type *P show the address;
  • a reference &T is followed, since it is a value, and shows as its referent;
  • a callable shows as <fn>, a weak T as weak(…) without following it, unit as () and a task as <task>;
  • device memory is shown by its type and never read: a volatile field, or an array or a view of them, inside a structure shows as <volatile u32> and the like, since reading a register can have side effects;
  • an opaque struct gets no supplied Debug, since its layout belongs to its own module. It is held through a pointer, which shows its address; one shown by value where its layout is private shows as its type in angle brackets, as does any value whose type has no Debug at all;
  • a trait object shows its concrete value: every object’s table carries a Debug slot after its own members, whether or not the trait requires Debug, so print of a *Shape uses the Debug of the Rect behind it. Two kinds of table carry none: a callable’s, shown as <fn>, and a Writer‘s, shown as <writer>. A sink is where a rendering goes rather than a value to show, and a slot in every writer’s table would pull a Debug for every sink into every program that writes to one. The slot costs one word per table, and keeps the concrete type’s Debug in the program wherever the table is.

A sink that should show itself says so in its trait: a trait requiring Debug beside Writer restores the slot, so its objects print the concrete sink while a bare &Writer stays <writer>. A concrete writer that is never erased prints through its Debug like any other value.

struct Tally
    n: usize

impl Fallible for Tally

impl Writer for Tally
    write(*self, bytes: []const u8)
        self.n = self.n + bytes.len

trait LoggedWriter: Writer + Debug

impl LoggedWriter for Tally

val w: &Writer = Tally(1)
val l: &LoggedWriter = Tally(2)

print(w, l, Tally(3))
<writer> Tally(n: 2) Tally(n: 3)

Printing a placeholder directly produces a warning, and still builds. A value whose whole printed form is <writer>, <fn>, <task> or a <Type> with no Debug, passed directly to print, to_string, an interpolation or format hole, debug_string, or an argument meeting a T: Debug bound such as assert_eq‘s, was almost certainly meant to show something. The print(w) above reports:

warning: printing 'w' shows only '<writer>': a Writer object carries no Debug; print the concrete
sink, or ask for 'Writer + Debug'

A placeholder inside a structure (a field, an element, an option’s payload) is part of a larger rendering and never produces the warning, and an object whose trait requires Debug has nothing to warn about. There is no switch to silence the warning; applying the fix it names removes it.

A rendering that nests deeper than thirty-two levels writes … and stops. Following references lets a list or a tree show in full, but would also make a structure that refers back to itself render forever; the limit makes every value safe to show.

struct Node
    v: int
    next: Option[&Node]

val list: &Node = Node(1, Some(Node(2, None)))

print(list)
Node(v: 1, next: Some(Node(v: 2, next: None)))

A trait may take type parameters

A trait declares parameters in the same bracketed list as every other generic declaration, and an implementation states which arguments it supplies:

trait Sink[T]
    put(*self, x: T) -> int

struct Buffer
    n: int

impl Sink[int] for Buffer
    put(*self, x: int) -> int
        self.n += x

        self.n

var b = Buffer(0)

print(b.put(3), b.put(4))
3 7

Sink is not one trait but a family of them: Sink[int] and Sink[string] say different things, and the implementation states which one a Buffer satisfies. This lets a trait describe a relation between two types rather than a property of one: what a sink accepts, what a conversion converts from, what an iterator yields.

The arguments are written the same way in all three positions where a trait is named, and mean the same thing in each:

positionwritten
a boundf[X: Sink[int]](x: X), and the body’s x.put(…) then takes an int
an implementationimpl Sink[int] for Buffer
a trait object&Sink[int]

A trait’s own parameters carry bounds too (trait Get[T: Show]), and every application of the trait must satisfy them, exactly as with a bounded struct.

A parameter may carry a default, and Self is its main use: in trait Scale[R = Self], R is the operand type, usually the implementing type, so impl Scale for P means impl Scale[P] for P and [T: Scale] asks for Scale[T]. See generics for how defaults are filled.

One implementation per argument list

A type may implement a trait once for each argument list, and the argument list distinguishes two implementations:

trait Sink[T]
    put(self, x: T) -> string

struct Buf
    tag: string

impl Sink[int] for Buf
    put(self, x: int) -> string = self.tag + " int"

impl Sink[string] for Buf
    put(self, x: string) -> string = self.tag + " string"

var b = Buf("b")

print(b.put(1), b.put("x"))
b int b string

A second impl Sink[int] for Buf beside those is refused as a duplicate.

This is the one qualification of the rule that a trait’s members become the type’s and that a type’s members share one namespace, and it is narrow. A type implementing both From[int] and From[real] has two members called from, and the argument list decides which one a use means. Every way of reaching a member already carries one: an operator has its pair of operands, a bound names the arguments, a trait object is formed at written arguments, and a named call passes values whose types are the arguments.

The resolution is exact, not ranked. A call is answered by the one implementation whose parameters are the types of the arguments, and a call matching none of them, or more than one, is reported rather than resolved. So c.mul(2), where the candidates take a Complex and a real, calls the real one, because an integer literal can be read as a real and cannot be a Complex. Where the candidates take an f32 and a real, a bare literal fits both and takes its default float type, real. Beyond that nothing ranks candidates: picking the nearest one would be a specialization rule, which the language does not have.

One limit follows from distinguishing several implementations inside one namespace: a property has no arguments, so when two implementations both supply one there is nothing to select with, and reading it is refused. The same set takes in what a block for a whole shape gives a type of that shape — see A shape and a type of that shape.

A generic block may write its own parameter as a trait argument: impl[T] Index[usize, T] for Buf[T] says a Buf[int] implements Index[usize, int] and nothing else. This lets a container carry the type it holds in the trait it implements, without an associated type, and it is the right tool when the container’s own parameters already name that type. The block’s parameters are exactly the arguments of the type it is written for, so an argument built from them means one thing per instantiation, and the subject settles which. That includes an argument that is the subject itself, which is how an operator carries a result that is not its operands’ type.

A generic block may not write an argument fixed to one instantiation of its own subject. Where the trait’s default names the type being asked about, such an argument coincides with the defaulted block at that one instantiation and differs from it at every other, which would be a choice between implementations rather than a lookup:

struct Box[T]
    v: T

impl[T] Mul[Box[int]] for Box[T]
    mul(self, rhs: Box[int]) -> Box[T] = self

print(1)
whose arguments default names the type it is written for

A shape and a type of that shape

A type’s members are one set, whatever wrote them. A block written for a shape (impl[E] … for []const E, every slice) and a block written for one type of that shape (impl … for []const int) may each give that type a member of one name, and a call weighs every candidate from both as one overload set, by the rules above:

trait Every[T]
    tag(self, x: T) -> string

impl[E] Every[E] for []const E
    tag(self, x: E) -> string = "an element"

trait Named
    tag(self, x: string) -> string

impl Named for []const int
    tag(self, x: string) -> string = "a name"

val xs: []const int = [1, 2]
val bs: []const u8 = [1]

print(xs.tag(3), xs.tag("n"), bs.tag(u8(3)))
an element a name an element

At the same parameter types, the written-out type’s member hides the shape’s. It is the more specific of the two, so it answers for that one type and the shape’s answers for every other — in whichever order the two blocks are written:

trait Show
    show(self) -> string

trait Count
    show(self) -> int

impl[T] Show for []T
    show(self) -> string = "every slice"

impl Count for []int
    show(self) -> int = 1

var store = [1, 2]
val ints: []int = store[..]
val words: []string = ["a"]

print(ints.show(), words.show())
1 every slice

This is what lets the library’s Elements give every slice contains(x) while a []const u8 keeps sysl.text‘s Search, whose contains looks for a run of bytes: bytes.contains(u8('b')) and bytes.contains("bc".bytes) each reach their own, and the starts_with both write at a []const u8 is the byte search’s.

A call both owners take at different parameter types, and that nothing narrows, is ambiguous, and the refusal names each owner:

trait Every[T]
    f(self, x: T) -> string

impl[E] Every[E] for []const E
    f(self, x: E) -> string = "every slice"

trait Wide
    f(self, x: u16) -> string

impl Wide for []const u8
    f(self, x: u16) -> string = "bytes"

val bs: []const u8 = [1]

print(bs.f(3))
'[]const byte.f' is ambiguous here — 2 members of that name, given by different owners, take these arguments:

A property is the exception, as it is inside one type: it is read with no arguments, so a shape and a type of it may not both have one of a name, nor a property beside a method:

trait Sized
    size -> int

trait Measure
    size(self, x: int) -> int

impl Measure for []int
    size(self, x: int) -> int = x

impl[T] Sized for []T
    size -> int = 2

print(1)
'[]int' already has a member named 'size', and this 'impl' would give every slice one — a property is read with no arguments to tell the two apart

The set is the written-out type’s and its nearest shape’s — the slice shape for a slice, the shape of its length for an array. Two shapes of one array ([N]int and [N]E), or a written-out array and the every-array shape, keep the rule they always had: the nearer one’s member answers.

A trait may declare an associated type

A trait parameter is written where the trait is applied. An associated type is written by the implementation, and everything else reads it from the type:

trait Render
    render(self) -> string

trait Seq
    type Item: Render
    head(self) -> Self::Item

struct Words
    first: string

impl Render for string
    render(self) -> string = self

impl Seq for Words
    type Item = string
    head(self) -> Self::Item = self.first

show[S: Seq](s: S) -> string = s.head().render()

print(show(Words("hi")))
hi

type Item: Render declares the associated type and what the type filling it must implement. type Item = string fills it. Self::Item is the projection: the same :: that reads int::Max in expression position, used in type position. show‘s body may call render on what it gets because the trait requires that of Item, and nothing more.

That is the difference from trait Sink[T], and it has three consequences:

a trait parameteran associated type
who writes the argumentwhoever names the traitthe implementation
how many per typeone implementation per argument listone, and the subject settles it
how a use names itSink[int], everywhere the trait is namedT::Item, off the type

A trait may declare both. The parameters go in the bracketed list and the associated types among the members, and the two do not interfere.

The projection normalizes as soon as its subject is concrete

Self::Item inside a generic body is abstract: it stands for a type not yet chosen, and is allowed exactly what the trait’s bound on it provides. On a known type it is the type the implementation chose, and may be written wherever a type may:

trait Render
    render(self) -> string

trait Seq
    type Item: Render
    head(self) -> Self::Item

struct Nums
    first: int

impl Render for int
    render(self) -> string = "an int"

impl Seq for Nums
    type Item = int
    head(self) -> Self::Item = self.first

val n: Nums::Item = 41

print(n, n.render())
41 an int

The type is deliberately not hidden. Swift’s some View conceals the type because Swift offers no way to write it; Nums::Item is a way to write it, so concealing it would add a restriction rather than an abstraction. A generic caller still sees only the bound, which is where the abstraction matters, and the projection being abstract there provides it.

A projection stands before . the way the type it names does, so an associated function is called through it. In a generic body the call reaches what the bound promises: type Item: Make promises make, so M::Item.make() is checked once against Make, and each instantiation calls the implementation’s own. At a concrete type, Box::Item.make() is Cell.make():

trait Make
    make() -> Self

struct Cell
    v: int

impl Make for Cell
    make() -> Self = Cell(7)

trait Maker
    type Item: Make

struct Box
    n: int

impl Maker for Box
    type Item = Cell

build[M: Maker]() -> M::Item = M::Item.make()

print(build[Box]().v, Box::Item.make().v)
7 7

A function the bound does not promise is refused in the generic body, with the bound it would need, even where every type that will fill the projection happens to have one:

trait Make
    make() -> Self

trait Spare
    spare() -> Self

struct Cell
    v: int

impl Make for Cell
    make() -> Self = Cell(7)

impl Spare for Cell
    spare() -> Self = Cell(8)

trait Maker
    type Item: Make

spare_of[M: Maker]() -> M::Item = M::Item.spare()
'spare' needs 'M::Item: Spare'

A generic block answers with one type per instantiation

The block’s own parameters may appear in the associated type, exactly as they may in a trait argument. Each instantiation of the subject then settles it:

trait Render
    render(self) -> string

trait Seq
    type Item: Render
    head(self) -> Self::Item

struct Cell[T]
    v: T

impl[T: Render] Render for Cell[T]
    render(self) -> string = "[" + self.v.render() + "]"

impl Render for int
    render(self) -> string = "i"

impl Render for string
    render(self) -> string = self

struct Box[T]
    v: T

impl[T: Render] Seq for Box[T]
    type Item = Cell[T]
    head(self) -> Self::Item = Cell(self.v)

show[S: Seq](s: S) -> string = s.head().render()

print(show(Box(3)), show(Box("x")))
[i] [x]

Box[int]::Item is Cell[int] and Box[string]::Item is Cell[string]. Monomorphization keeps the concrete type throughout, so this has no run-time cost: there is no box per value and no indirect call.

A default’s callable may name the projection

A default body is checked once at the trait, where Self is a parameter, and copied per implementing type, where Self is that type. A bare arrow is shorthand for a bound rather than a parameter type, so a default taking one over Self::Item uses the projection where neither type is written out, and each copy binds it to what its own subject chose:

trait Mapper
    type Item

    first(self) -> Self::Item
    over[N](self, f: Self::Item -> N) -> N = f(self.first())

struct One
    v: int

impl Mapper for One
    type Item = int
    first(self) -> int = self.v

struct Word
    w: string

impl Mapper for Word
    type Item = string
    first(self) -> string = self.w

print(One(20).over(n -> n + 1))
print(Word("hello").over(s -> s.len))
21
5

n is an int and s is a string, from one body that names neither. The arrow has no cost at the call, since it monomorphizes and puts no closure on the heap. It is the member’s own type parameter N, not the projection, that keeps over out of the table.

some Trait — the result read off the body

The type an implementation chooses is often one nobody wants to write. A tree of nested containers is the typical case: the type is large, it is an implementation detail, and it changes whenever the body does. some Trait in an impl member’s result says “the associated type is whatever this body produces, and it implements this trait”:

trait Render
    render(self) -> string

trait View
    type Body: Render
    body -> Self::Body

struct Text
    s: string

impl Render for Text
    render(self) -> string = self.s

struct Counter
    n: int

impl View for Counter
    body -> some Render = Text("count")

draw[V: View](v: V) -> string = v.body.render()

print(draw(Counter(7)))
count

No type Body = … line is written; the body supplies the type. Counter::Body is Text, so the two forms are the same feature: one writes the type, the other reads it from the body.

some is a contextual keyword, special only in front of a bound in a result position. A program may still name a variable, a parameter or a function some.

Every path out of the member must produce one type. Two branches yielding different concrete types are an error, not a silent widening to the bound: the bound is what a caller may rely on, not what the member returns. No separate rule is needed, since an if used as a value must already have matching branches, and that rule reports it.

trait Render
    render(self) -> string

trait Seq
    type Item: Render
    head(self) -> Self::Item

struct A
    n: int

struct B
    n: int

impl Render for A
    render(self) -> string = "a"

impl Render for B
    render(self) -> string = "b"

struct Box
    flag: bool

impl Seq for Box
    head(self) -> some Render = if self.flag then A(1) else B(2)

print(1)
if branches have different types: A and B

The type may be one no other file can name. A private struct supplied through a some result works and is opaque outside the file that declares it: a caller reaching it through a bound can do what the bound allows and has no way to write the type. Swift’s some View achieves this by concealment; sysl achieves it with ordinary visibility.

some is allowed only in that position. A free function has no trait whose associated type it could supply, so the refusal says where some belongs:

trait Render
    render(self) -> string

f(x: int) -> some Render = x

print(1)
'some Render' says the type is read off the body of a member that supplies a trait's associated type, so it stands only in an 'impl' block

The bound is checked against what the body actually produces:

trait Render
    render(self) -> string

trait Seq
    type Item
    head(self) -> Self::Item

struct Box
    v: int

impl Seq for Box
    head(self) -> some Render = self.v

print(1)
'head' promises 'some Render' and its body yields int, which does not implement 'Render'

What the implementation is held to

A written binding is checked against the trait’s bound in the same way:

trait Render
    render(self) -> string

trait Seq
    type Item: Render
    head(self) -> Self::Item

struct Box
    v: int

impl Seq for Box
    type Item = int
    head(self) -> Self::Item = self.v

print(1)
trait 'Seq' asks that its associated type 'Item' implement 'Render', and int does not

An implementation that supplies no associated type has not implemented the trait, exactly as with a missing method:

trait Seq
    type Item
    head(self) -> Self::Item

struct Box
    v: int

impl Seq for Box
    head(self) -> Self::Item = self.v

print(1)
'Box' does not implement 'Seq': the associated type 'Item' is missing

Self inside such a bound is the associated type, not the type implementing the trait. A bound constrains what it is written on, and type W: Add is written on W, so the defaults of Add, which are Self, are filled from W. type W: Add on an implementation supplying type W = u32 therefore asks whether u32 is Add[u32, u32], which it is, and not whether it can be added to the implementing type:

trait Holder
    type W: Add
    one(self) -> Self::W

struct N
    v: u32

impl Holder for N
    type W = u32
    one(self) -> u32 = self.v

sum[H: Holder](h: H) -> H::W = h.one() + h.one()

print(sum(N(21)))
42

This is how Self reads in a bound everywhere; it is stated here because an associated type is the one place where two types are in play and either would be a plausible reading.

The bound is checked once every impl in the program is registered, so the block that makes the supplied type implement the bound may be written below the block that chose it. A required trait follows the same rule, for the same reason.

One name per type, because a projection does not name its trait

Box::Item names a type and a name, never a trait. So a type may implement at most one trait declaring an associated type of a given name, and the second block is refused:

trait A
    type Item
    a(self) -> Self::Item

trait B
    type Item
    b(self) -> Self::Item

struct Box
    v: int

impl A for Box
    type Item = int
    a(self) -> Self::Item = self.v

impl B for Box
    type Item = bool
    b(self) -> Self::Item = true

print(1)
one type cannot have two of one name

Rust writes the qualified form <T as A>::Item; sysl has no such form, so the collision is refused rather than left to be disambiguated at every use.

Declaring one spends the trait’s erasability, unless the object says which type it is

An erased value does not know which type it is, and an associated type depends on exactly that, so the slot would have a different signature for every implementing type. A bare &Seq therefore cannot be formed, and the refusal names the associated type rather than leaving it to surface as a Self:

trait Seq
    type Item
    head(self) -> Self::Item

look(s: &Seq) -> int = 0

print(1)
'Seq' declares the associated type 'Item', whose meaning is the implementing type's — an erased value has forgotten which type that is, so there is no '&Seq' to form

A bound keeps the type, and with it the associated type. [S: Seq] is the intended use of such a trait, as it is for the operator catalog, for a related reason. A trait that requires such a trait cannot be erased either, and the diagnostic names the trait the associated type comes from.

An object may fix the associated type

The alternative is to write the type out. &Seq[Item = int] is a value of some unknown type whose Item is known to be int, so every slot has one signature again, and there is a table to point at:

trait Seq
    type Item
    head(self) -> Self::Item

struct Box
    v: int

impl Seq for Box
    type Item = int
    head(self) -> Self::Item = self.v

show(s: &Seq[Item = int])
    print(s.head())

show(Box(7))
7

Where a trait has no parameters of its own and exactly one associated type, the name may be left off. &Seq[int] and &Seq[Item = int] are then the same type, not two types with a conversion between them, so a function declared with either accepts a value of the other:

trait Seq
    type Item
    head(self) -> Self::Item

struct Box
    v: int

impl Seq for Box
    type Item = int
    head(self) -> Self::Item = self.v

named(s: &Seq[Item = int]) -> int = s.head()
bare(s: &Seq[int]) -> int = named(s)

print(bare(Box(4)))
4

The short form is not available where it would be ambiguous. A trait with parameters and an associated type takes its arguments in order, first, and names the associated type after them (&Keyed[int, Item = string]), because a bare argument there means one of the trait’s own:

trait Keyed[K]
    type Item
    at(self, k: K) -> Self::Item

struct Row
    v: int

impl Keyed[int] for Row
    type Item = int
    at(self, k: int) -> Self::Item = self.v + k

show(s: &Keyed[int, Item = int])
    print(s.at(2))

show(Row(5))
7

The value put into the object must have chosen the same type. This makes the binding sound: every slot’s signature was read under the object’s binding, so a value whose implementation chose otherwise would be called through a table with the wrong types. It is refused at the erasure, the one place where both are known:

trait Seq
    type Item
    head(self) -> Self::Item

struct Box
    v: int

impl Seq for Box
    type Item = int
    head(self) -> Self::Item = self.v

show(s: &Seq[Item = string])
    print(s.head())

show(Box(7))
a &Seq[Item = string] says 'Item' is string, and Box supplies int for it — an object fixes the associated type, so only a type that chose the same one goes into it

A binding is checked against whatever the trait requires of the associated type, exactly as an implementation is. A required trait’s associated type is bound in the same brackets, under its declared name, since its members are slots in the same table.

A bound may fix the associated type too

A bound fixes an associated type exactly as an object does: by name, after the trait’s own arguments, a required trait’s under its declared name, and — for a trait with no parameters and one associated type — by a lone bare argument. Inside the body the projection is the type the bound wrote, not a type known to equal it, so S::Error and E below are one type, and ? hands the store’s error back as the function’s own. The type it fixes may be another of the declaration’s parameters, and a call infers that one from the argument’s implementation:

trait Source
    type Error
    get(self) -> Result[int, Self::Error]

enum Fault
    Empty

struct Box
    v: int

impl Source for Box
    type Error = Fault
    get(self) -> Result[int, Self::Error] = if self.v == 0 then Err(Fault.Empty) else Ok(self.v)

// The error is named, so the result can say it.
doubled[S: Source[Error = Fault]](s: S) -> Result[int, Fault]
    val v = s.get()?
    Ok(v * 2)

// The error is a parameter of its own, settled by what 'S' chose.
tripled[S: Source[Error = E], E](s: S) -> Result[int, E]
    val v = s.get()?
    Ok(v * 3)

print(doubled(Box(21)).unwrap(), tripled(Box(2)).unwrap())
print(doubled(Box(0)))
42 6
Err(Fault.Empty)

A type meets such a bound only where its implementation chose the same type, which is what makes the body’s assumption sound. It is refused at the call, where both are known, as an object refuses the value erased into it:

trait Source
    type Error
    get(self) -> Result[int, Self::Error]

enum Fault
    Empty

enum Other
    Gone

struct Box
    v: int

impl Source for Box
    type Error = Other
    get(self) -> Result[int, Self::Error] = Ok(self.v)

doubled[S: Source[Error = Fault]](s: S) -> Result[int, Fault]
    val v = s.get()?
    Ok(v * 2)

print(doubled(Box(21)))
'doubled' requires its type parameter 'S' to implement 'Source[Error = Fault]', which says 'Error' is Fault, and Box supplies Other for it — a bound that fixes an associated type is met only by a type that chose the same one

A type parameter handed on meets it only where its own bound fixed the same type; one bounded by the bare trait leaves the type open, and is refused rather than assumed. A binding is held to the trait’s declaration as an object’s is — each name declared, each named once, each type meeting what the trait requires of it (type Item: Display) — and it applies wherever a bound is written: a function, a where clause, a generic type, an impl, a trait’s own member.

A requirement may fix the associated type

A trait’s requirement list fixes one the same way, and then a body bounded by the requiring trait reads the projection as the fixed type, through however many requirements it is reached:

trait Seq
    type Item
    head(self) -> Self::Item

trait Doubles: Seq[Item = int]
    doubled(self) -> int

struct Box
    v: int

impl Seq for Box
    type Item = int
    head(self) -> Self::Item = self.v

impl Doubles for Box
    doubled(self) -> int = self.v * 2

twice[T: Doubles](t: T) -> int = t.head() + t.doubled()

print(twice(Box(5)))
15

A requirement has no call to refuse at, so every implementation of the requiring trait is held to it, at the impl:

trait Seq
    type Item
    head(self) -> Self::Item

trait Doubles: Seq[Item = int]
    doubled(self) -> int

struct Words
    v: string

impl Seq for Words
    type Item = string
    head(self) -> Self::Item = self.v

impl Doubles for Words
    doubled(self) -> int = 2

print(1)
trait 'Doubles' requires 'Seq[Item = int]', which says 'Item' is int, and Words supplies string for it — every implementation of 'Doubles' has to choose the same one

A generic impl meets it only where its own bound fixes the same type. Two routes to one trait that fix its associated type differently (trait Both: Ints + Strs, with Ints: Seq[Item = int] and Strs: Seq[Item = string]) are refused at the declaration, since no type could choose both.

A bound may ask something of the associated type instead

Seq[Item: Render] leaves the associated type open and asks a trait of it, with the : and + a type parameter’s bound is written with. The body may then use what that trait provides on a S::Item, as it may use what the trait’s own type Item: … provides. It is Rust’s Seq<Item: Render>, and it may stand wherever a bound may: a function, a where clause, a generic type, an impl. Several traits are joined with + (Item: Render + Eq), a fixed associated type and a bounded one may share a list (Pair[Key = int, Value: Render]), and a bound may hold a bound of its own (Seq[Item: Lanes[Lane: Render]]).

A requirement list may carry one too, which is what a trait that tightens another needs. trait Scalar: Element[Real: Places] asks every implementation of Scalar that the Real its Element chose implement Places, so a default body in Scalar, and any body bounded by it, may call places on a Self::Real:

trait Places
    places(self) -> int

trait Element
    type Real
    real(self) -> Self::Real

trait Scalar: Element[Real: Places]
    describe(self) -> string = s"${self.real().places()} places"

impl Places for f64
    places(self) -> int = 15

struct D
    v: f64

impl Element for D
    type Real = f64
    real(self) -> Self::Real = self.v

impl Scalar for D

total[T: Element[Real: Places]](x: T) -> int = x.real().places() + 1

print(D(1.5).describe(), total(D(2.5)))
15 places 16

Unlike fixing it, bounding it in a requirement asks nothing a call has to check: the type stays the implementation’s choice, and the question is put to every impl of the requiring trait instead, once every block in the program is registered:

trait Places
    places(self) -> int

trait Element
    type Real
    real(self) -> Self::Real

trait Scalar: Element[Real: Places]
    one(self) -> int = 1

struct F
    v: f32

impl Element for F
    type Real = f32
    real(self) -> Self::Real = self.v

impl Scalar for F

print(1)
trait 'Scalar' requires 'Element[Real: Places]', which asks that its associated type 'Real' implement 'Places', and F supplies f32 for it, which does not

A bound on a parameter is held at the call, as a fixed one is, and a type parameter handed on meets it only where its own bounds ask the same — [U: Scalar] does, [U: Element] does not:

trait Places
    places(self) -> int

trait Element
    type Real
    real(self) -> Self::Real

struct F
    v: f32

impl Element for F
    type Real = f32
    real(self) -> Self::Real = self.v

total[T: Element[Real: Places]](x: T) -> int = x.real().places()

print(total(F(1.0)))
'total' requires its type parameter 'T' to implement 'Element[Real: Places]', which asks that 'Real' implement 'Places', and F supplies f32 for it, which does not

Self inside such a bound is the associated type, as it is in the trait’s own type Item: Add, so [T: Element[Real: Add]] may add two T::Reals. A name is fixed or bounded, never both — the type it fixes is the whole answer — and its bounds are written together, once. An object cannot bound one: &Element[Real: Places] has forgotten its type, so nothing could answer which Real to ask, and it is refused with the advice to fix the type or bound a parameter instead, as Rust refuses one in a dyn. An object or a bound that fixes an associated type a requirement bounds is held to that bound: &Scalar[Real = f64] is an object, and &Scalar[Real = f32] is refused, since no implementation of Scalar could have chosen f32.

A trait may require another trait

trait Word: Add + BitXor is written after the name, with the same : and + a bound uses, because it asks the same thing of the implementing type.

trait Named
    label(self) -> string

trait Describe: Named
    describe(self) -> string = "device " + self.label()

struct P
    n: string

impl Named for P
    label(self) -> string = self.n

impl Describe for P

var p = P("P-101")

print(p.describe())
device P-101

A required trait is stated once by the trait rather than repeated by each declaration that uses it. [T: Describe] then allows label; a default body in Describe may use it, since a default may assume exactly what its trait provides; and a &Describe object carries the required trait’s members in its table.

The requirement is checked at the impl, not at the bound, so the diagnostic lands on the declaration that fails to meet it:

trait Named
    label(self) -> string

trait Describe: Named
    describe(self) -> string = "device"

struct P
    n: string

impl Describe for P

print(1)
'Describe' requires 'Named', so 'P' has to implement that too — write 'impl Named for P'

Checking at the impl also keeps conformance a plain lookup: by the time anything asks whether a type implements a required trait, an implementation of it is already registered. The check waits until every impl has been seen, since the block supplying a required trait may be written below the one that needs it.

The table carries the required trait’s slots

A trait’s members are the required traits’ members, depth-first with each trait taken once, followed by its own. Both the table and the call sites that index into it are laid out from that one list, so a required trait’s method is one indirect call, exactly like the trait’s own. The alternative, a word in the table pointing at the required trait’s own table, would cost a second load on every such call and would only enable an upcast, which sysl does not have.

So a &Sub cannot become a &Super; that is the deliberate cost of this layout. No program is prevented by it: what a program does with a required trait is call its members, which works.

The diamond needs no rule of its own. D: A + C with both A: B and C: B carries B‘s members once, because the walk takes each trait the first time it reaches it. What is refused is two traits in one closure declaring a member of the same name, because of the table, not the namespace. Two unrelated traits may each give one type a member of the same name, because a call selects one by naming the trait; two traits inside one requirement closure are laid out as one table, and a call through a &Sub has nothing left that could say which slot it meant.

trait L
    len2(self) -> int

trait R
    len2(self) -> int

trait Both: L + R
    both(self) -> int

print(1)
'R' and 'L' both declare 'len2', and a trait's members become the implementing type's — so 'Both' cannot require both

A trait may not require itself, directly or around a cycle:

trait Loop: Loop
    step(self) -> int

print(1)
trait 'Loop' requires itself, through Loop -> Loop

Two more rules apply. A trait may not require one that is less visible than itself, since implementing the trait means implementing the required one, and a requirement the implementer cannot name would make the trait impossible to implement from outside. And Self in a requirement’s arguments is the type implementing the requiring trait, so trait Vector: Scale[Self] asks that whatever implements Vector can be scaled by its own type, which is the same requirement trait Vector: Scale states when Scale defaults its parameter to Self.

Trait objects

A trait object is a fat pointer: two words, the method table for the erased type and the value itself:

{ ptr vtable, ptr data }

The sigil says who owns the second word; nothing else differs between the two:

writtenthe data word iswho frees it
*Traitthe value’s own addressnobody — raw and unmanaged, like every *T
&Traitthe reference-counted box the value sits inARC, exactly as for the &T it was erased from

*T and &T on a concrete type are thin pointers; on a trait they are fat. Naming a trait is what makes them fat, which is why there is no dyn.

There is one table per (trait, type, sigil), two variants rather than one because the data word means different things: an entry has to reach a receiver, and from a box that is one step further in than from a bare value. Where the data word already is the receiver an implementation declares, the entry names that implementation directly; otherwise it names a small adapter that steps over the box header, loads the value, or both. The common case costs one indirect call and nothing more.

A table is emitted only where a function the program reaches erases a value into it, together with the functions it points at. The standard library’s own tables follow the same rule: a library body that creates an object but is never reached keeps neither the table nor the functions it names, so a board image that never creates a Buf carries no malloc because of Buf‘s Writer table. Erasing a library type in the program’s own code keeps that type’s table:

main()
    var c = Counting(0)
    val w: *Writer = &c

    w.write("abc".bytes)
    print(c.n)
3

Two objects compared by identity

=== and !== on a trait object compare its data word — the value it was made from — so two objects over one value are identical whichever trait each was made against, and the table beside the word plays no part. An object also meets a reference of the type it holds, on either side, as &Trait against &T and *Trait against *T:

trait Shape
    area(self) -> int

trait Named
    name(self) -> string

struct Sq
    s: int

impl Shape for Sq
    area(self) -> int = self.s * self.s

impl Named for Sq
    name(self) -> string = "sq"

main()
    val s: &Sq = Sq(2)
    val a: &Shape = s
    val n: &Named = s
    val other: &Shape = Sq(2)

    print(a === n, a === s, a === other, n !== other)
true true false true

The two handles must be of one kind — two references of one sharing, or two pointers — and a concrete type must implement the object’s trait, since otherwise the two never name one value:

trait Shape
    area(self) -> int

struct Sq
    s: int

impl Shape for Sq
    area(self) -> int = self.s * self.s

struct Plain
    v: int

main()
    val s: &Shape = Sq(1)
    val p: &Plain = Plain(1)

    print(s === p)
'===' compares a &Shape with a &Plain, and Plain does not implement Shape, so the two never name one value

An object has no ==: equality would need the two values’ types to agree, which the objects have forgotten. Identity is what an object can be asked, and what a search for one in a list of objects wants.

Object safety

Erasure discards the type, so no member may depend on knowing it. A trait can be made into an object when it declares no associated type (an associated type depends on exactly the erased type) and when every member:

  • has a receiver. An associated function has nothing to dispatch on. A property does have one, by value and unwritten, so a trait requiring a property is as safe to erase as one requiring a method.
  • mentions Self only in that receiver. A second Self would have to be the same erased type as the first, which is exactly what an object does not record, and a Self result has no known size to return.
  • does not take &self, for a *Trait only. &self requires its receiver inside a box, and a raw object points directly at a value. A &Trait carries a box, so it accepts such a member.
  • takes no .... A call to a variadic names the callee’s whole function type, because that is how it marks where the declared parameters stop and the variable tail begins, and a table slot is one word and names no type.
trait Scale
    scale(self, k: int) -> Self

struct P
    v: int

impl Scale for P
    scale(self, k: int) -> P = P(self.v * k)

grow(s: &Scale) -> int = 0

print(1)
'scale' of 'Scale' mentions 'Self' away from its receiver, and an erased value has forgotten which type that is — so there is no '&Scale' to form

The second rule excludes every trait in the operator catalog, starting with add(self, rhs: Self) -> Self. This is correct rather than a limitation: those traits describe an operator over two values of one type, which concerns types known at compile time. They are meant for bounds.

A trait that requires an unerasable trait is itself unerasable, and the diagnostic names the trait the offending member came from. A built-in type that satisfies a requirement by the compiler’s rule cannot be erased through it either, since a table holds function pointers and a scalar’s add is an instruction.

This affects only the operator catalog at written arguments. Add[int, int] declares add(self, rhs: int) -> int, with no Self, so it is a valid object type, and an int implements it by the compiler’s rule. &Add[int, int] = 3 is therefore refused, and the diagnostic explains why rather than reporting a plain mismatch.

Display and Hash are not affected: every built-in type implements both through an impl, so a *Display holds an int, a u256, a string or a float alike, a &Hash holds anything that hashes, and a heterogeneous array of either is ordinary code. The rest of the catalog (Eq, Ord, Bits, Signed) mentions Self outside the receiver, so object safety refuses the type before a value is involved.

A member with its own type parameters leaves the table, not the trait

The four rules above decide whether a trait has an object type at all. A member that declares type parameters of its own is a different, milder case: it is not a function until a call names its types, so no slot can point at it, and the other members are unaffected. The object can still be formed, and it dispatches them:

trait Applies
    tag(self) -> int
    apply[U](self, f: &Fn(int) -> U) -> U

struct N
    v: int

impl Applies for N
    tag(self) -> int = self.v
    apply[U](self, f: &Fn(int) -> U) -> U = f(self.v)

reach[T: Applies](x: T) -> string = x.apply(n -> s"<${n}>")

val o: &Applies = N(3)

print(o.tag())
print(N(3).apply(n -> s"<${n}>"))
print(reach(N(4)))
3
<3>
<4>

Of the three ways to reach that member, only one is closed. On the value it is an ordinary call; a bound solves the member’s own parameters at the instantiation, as reach does; the object cannot reach it, because it dispatches through a table:

trait Applies
    tag(self) -> int
    apply[U](self, f: &Fn(int) -> U) -> U

struct N
    v: int

impl Applies for N
    tag(self) -> int = self.v
    apply[U](self, f: &Fn(int) -> U) -> U = f(self.v)

val o: &Applies = N(3)

print(o.apply(n -> s"<${n}>"))
'apply' of 'Applies' declares type parameters of its own, so it is not a function until a call names them

The consequence for bounds: an object does not satisfy a bound on such a trait, because a bound requires every member and the object reaches only its table (generics). That is the one exception to an otherwise universal rule, and it is described there.

This is what lets a trait carry a map. A member choosing its own result type is what makes sysl.seq possible (map[U] names U at the call and nowhere else), and the cost is a member no object dispatches, borne only by traits that declare one.

A bare-arrow callable is the same case in different syntax, and the most common one in traits. f: T -> U is shorthand for a type parameter bounded by Fn(T) -> U (types), so a member written that way declares a type parameter and loses its slot. sysl.seq makes that trade deliberately: nothing is boxed at any call into it, and &Sequence[int] dispatches none of its members. The boxed form keeps the slot, so a trait meant to be erased uses it.

Forming and using one

Erasure is a coercion, applied wherever a trait-object type is expected: an argument, a declared variable, an assignment, a returned value, an array element, a struct field. &r erases to *Shape; a &Rect erases to &Shape; and a plain Rect(3, 4) where a &Shape is expected is boxed and then erased, which is the ordinary rule that a construction where a &T is expected is allocated, with one more step.

A *Trait does not accept a bare value. A raw pointer needs an address, and taking one silently would let a program acquire a dangling pointer with no line to show for it:

trait Shape
    area(self) -> int

struct Rect
    w: int

impl Shape for Rect
    area(self) -> int = self.w

var s: *Shape = Rect(2)

print(s.area())
a *Shape points at a value, so it needs an address — write '&' in front of the Rect to take one

Writing the & is all it takes: &Rect(2) stores the value in a hidden local of that scope and yields its address, so the fix the diagnostic names is one character rather than a second declaration. Only the silent case above is refused; see Addressing a value.

Because the coercion applies per branch, an if or a match whose arms have different concrete types can produce one trait object:

trait Shape
    area(self) -> int

struct Rect
    w: int
    h: int

struct Square
    s: int

impl Shape for Rect
    area(self) -> int = self.w * self.h

impl Shape for Square
    area(self) -> int = self.s * self.s

var wide = true
var s: &Shape = if wide then Rect(2, 3) else Square(4)

print(s.area())
6

What an object offers is the trait’s members and nothing else: no dereference, no fields, no comparison.

trait Shape
    area(self) -> int

struct Rect
    w: int
    h: int

impl Shape for Rect
    area(self) -> int = self.w * self.h

var s: &Shape = Rect(2, 3)

print(s.w)
a &Shape has no fields, and trait 'Shape' declares no 'w'

A call is checked against the trait’s signature, which represents every implementation because conformance is exact.

An object keeps one trait and what that trait requires. A bound may name several traits because a bound is a list; a trait-object type names one because it is a type. So a value implementing Shape and Display keeps only Shape when it becomes a &Shape, unless Shape requires Display, which decides whether the object is printable. A multi-trait object type (&(Shape + Display)) would be a second, worse way to say the same thing, since it repeats at every use a fact that belongs on the trait.

An object of a type with a destructor

A type with a Drop is a reference type (memory), so building one where an object is expected boxes it once, into the object, and the destructor runs when the last object goes — exactly once, however it was reached:

trait Shape
    area(self) -> int

struct Lamp
    n: int

impl Drop for Lamp
    drop(self) = print("off", self.n)

impl Shape for Lamp
    area(self) -> int = self.n * 10

measure(s: &Shape) -> int = s.area()

show()
    val s: &Shape = Lamp(1)

    print("area", s.area(), measure(Lamp(2)))

show()
print("done")
area 10 20
off 2
off 1
done

What may not go into one is a copy. *r read out of a &Lamp is a second value standing for the same lamp, and an object made from it would be a second owner, so it is refused as boxing it into a &Lamp is — hand over the reference itself, which erases without a copy:

trait Shape
    area(self) -> int

struct Lamp
    n: int

impl Drop for Lamp
    drop(self) = print("off", self.n)

impl Shape for Lamp
    area(self) -> int = self.n * 10

show(r: &Lamp)
    val s: &Shape = *r
    print(s.area())

show(Lamp(1))
this makes a '&Lamp' out of a copy of a 'Lamp' value, and 'Lamp' has a 'Drop'

The two sigils do not convert

*Trait and &Trait are two types, and neither is accepted for the other.

In the useful direction, lending a counted object to code that only reads it, this is stricter than for plain references, which have a form for it: &*r is the address of the place *r, so a &T reaches a function written against *T with the step into the unsafe tier written at the call. An object has no dereference, so &*o has no meaning, and a function that only reads a shape must be written once per sigil. This is an open gap in the language: what is missing is a form, and any form must keep the crossing searchable.

The other direction is refused for a stronger reason: a raw object points at a value with no count to take a share of, so accepting one where a counted object is expected would be inventing ownership.

An object cannot be erased a second time

trait Shape: Display puts the slots of Display in a &Shape‘s table, and the object satisfies a [T: Display] bound, but it cannot become a &Display. A bound asks what may be called through the value, and the table answers it. Forming an object asks what may be assembled from the value’s type, and a table is built from a type’s implementations, which an object does not have.

trait Shape: Display
    area(self) -> int

struct Rect
    w: int
    h: int

impl Shape for Rect
    area(self) -> int = self.w * self.h

impl Display for Rect
    display(self, out: *Writer, fmt: FormatSpec) = display_pad("a rect".bytes, out, fmt)

var o: &Shape = Rect(3, 4)
var d: &Display = o
a &Shape has forgotten which type it holds, so there is nothing for a &Display to be built from

The members of Display are in that table, laid out inline so that a required trait’s method costs the same one indirect call as the trait’s own methods. What is missing is a name for that run of slots. Flattening gives up the upcast in exchange for one indirection instead of two on every call through a &Shape.

Asking an object which type it holds

An object has forgotten its type, but it can still be asked about one. Every trait object has two members the compiler supplies: o.is[T]() answers whether the type inside it is exactly T, and o.as[T]() answers an Option[&T], which is the object itself as a &T when it holds a T and None when it holds anything else:

trait Shape
    area(self) -> int

struct Rect
    w: int
    h: int

struct Sq
    s: int

impl Shape for Rect
    area(self) -> int = self.w * self.h

impl Shape for Sq
    area(self) -> int = self.s * self.s

var xs: [3]&Shape = [Rect(2, 3), Sq(4), Rect(1, 5)]
var wide = 0

for x in xs
    if x.as[Rect]() is Some(r) then wide += r.w
    if x.is[Sq]() then print("a square of", x.area())

print(wide)
a square of 16
3

Both read the identity the object already carries, the ::Id its table begins with, so is costs one load and one comparison, and as the same plus a count on a hit.

The answer is the object, not a copy of it. The data word of a &Shape is the very box the &Rect it was made from pointed at, so as hands that box back with a count taken for the caller. A write through the answer is seen through the object, and a type with a destructor is still destroyed once, when the last reference of either kind goes:

import sysl.buf.{Buf, buf}

trait Shape
    area(self) -> int

struct Lamp
    n: int

impl Drop for Lamp
    drop(self) = print("off", self.n)

impl Shape for Lamp
    area(self) -> int = self.n

main()
    var xs: Buf[&Shape] = buf()

    xs.push(Lamp(7))

    val kept = xs[0].as[Lamp]()

    xs.clear()
    print("cleared")
    print(kept.unwrap().n)
cleared
7
off 7

The test is for exactly one type, fully instantiated. Col[int] and Col[real] are two types, so an object holding one is not the other. A generic body asks through its parameter, and the type asked about is the one each instantiation names — the shape a table of type-erased columns needs:

trait Column
    len(self) -> int

struct Col[T]
    items: [4]T

impl[T] Column for Col[T]
    len(self) -> int = 4

column[T](c: &Column) -> Option[&Col[T]] = c.as[Col[T]]()

var c: &Column = Col[int]([1, 2, 3, 4])

print(c.is[Col[int]](), c.is[Col[real]]())
print(column[int](c).unwrap().items[2], column[real](c).is_none())
true false
3 true

What an object holds is a concrete type, the one erased from a &T, so a trait is never the answer, and an object is not cast from one trait to another:

trait Shape
    area(self) -> int

struct Rect
    w: int

impl Shape for Rect
    area(self) -> int = self.w

var s: &Shape = Rect(2)

print(s.as[Shape]())
'Shape' is a trait, and what a &Shape holds is the concrete type its value was made as, so 'o.as[T]()' asks for that type — an object is not cast to another trait

A type that does not implement the trait could never be inside one of its objects, so asking for it is a mistake rather than a question whose answer is always no:

trait Shape
    area(self) -> int

struct Rect
    w: int

struct Circle
    r: int

impl Shape for Rect
    area(self) -> int = self.w

var s: &Shape = Rect(2)

print(s.is[Circle]())
'Circle' does not implement 'Shape', so no &Shape can hold one and 'o.is[Circle]()' could never be true

A reference is never the answer either (as[&Rect] is refused), and inside a generic body the same checks are made of each instantiation’s type.

The sigil carries over. A &sync Shape answers an Option[&sync Rect], counted atomically as the object is. A *Shape is asked the same two questions, and its as answers an Option[*Rect]: the object’s own address, borrowed, with no count taken because none is kept for it:

trait Shape
    area(self) -> int

struct Rect
    w: int

impl Shape for Rect
    area(self) -> int = self.w

var r = Rect(2)
val p: *Shape = &r

print(p.is[Rect]())

if p.as[Rect]() is Some(q) then print(q.w)
true
2

A member the trait declares wins. is is not a reserved word, so a trait may declare a member of that name, and then o.is() calls it; the compiler’s is is the fallback, as every form the compiler supplies is.

A type is not a pattern, so the form a reader might try first does not test anything: s is Rect reads Rect as an ordinary binding, which matches every object. The test is written s.is[Rect](), and the test that binds is s.as[Rect]() is Some(r):

trait Shape
    area(self) -> int

struct Rect
    w: int
    h: int

impl Shape for Rect
    area(self) -> int = self.w * self.h

var s: &Shape = Rect(2, 3)

if s is Rect then print("yes") else print("no")
this pattern matches every &Shape, so the test is always true — take the value apart with 'match', or bind it with 'var'

The identity is readable

o::Id gives the identity of the type inside an object, as a usize. It is the one fact about the erased type that an object still carries, because the method table it points at begins with it:

trait Shape
    area(self) -> int

struct Rect
    w: int

struct Sq
    s: int

impl Shape for Rect
    area(self) -> int = self.w

impl Shape for Sq
    area(self) -> int = self.s

var a: *Shape = &Rect(2)

print(a::Id == Rect::Id, a::Id == Sq::Id)
true false

It is what is and as compare, and on its own it can only be compared: two values with the same id hold the same type. There is no map from an id back to a type and no Any. Read directly it serves the two things a collection of erased values typically needs beside a test: a key, for asking whether the node here is the same kind as the node that was here before, and the first half of a memo table’s key.

Two objects can therefore be asked whether they hold the same type without naming it, and a count of one kind needs no hand-maintained kind property returning a constant in every implementation:

trait Shape
    area(self) -> int

struct Rect
    w: int

struct Sq
    s: int

impl Shape for Rect
    area(self) -> int = self.w

impl Shape for Sq
    area(self) -> int = self.s

var xs: [3]*Shape = [&Rect(1), &Sq(2), &Rect(3)]
var rects = 0

for x in xs
    if x::Id == Rect::Id then rects += 1

print(rects)
2

A known type is asked with T::Id, and ::Id on a value is allowed only where the value is erased, the one case where it tells you something the static type does not:

struct Rect
    w: int

var r = Rect(2)

print(r::Id)
'::Id' on a value reads the identity an erased value carries

It guarantees only that equal ids mean the same type. It is not stable across compiler versions (it is derived from the type’s name, and the naming scheme may change), it is not a number to store anywhere, and two compilations of one program agree only because they compute the same value from the same name. A generic body asks through its parameter, T::Id, which is computed once per instantiation.

A method borrows what it is handed

A call through a trait object is opaque: which body it reaches is decided at run time, so the compiler cannot read out of a body whether the call keeps what it is handed. It answers by rule instead: a trait’s method borrows its parameters for the call — a view, a *T, or a struct holding either — and the compiler checks every implementation against the rule, so it is verified rather than trusted. A view of an array in your own frame passed through an object therefore stays in the frame (memory.md § A borrowed pointer cannot escape):

trait Sink
    put(*self, bytes: []const u8)

struct Counter
    n: usize

impl Sink for Counter
    put(*self, bytes: []const u8)
        self.n += bytes.len

var c: Counter
var s: *Sink = &c
var buf: [8]u8

buf[0] = 7
s.put(buf[0..<4])

print(c.n)
4

The frame keeps buf, which is what makes rendering allocation-free: a renderer writes into a buffer on its own stack and passes a slice of it to a sysl.Writer. A closure behind a &Fn is the one object that borrows nothing for a view — a closure may keep what it is handed — so a view passed through one goes on the heap.

An implementation that keeps what it was lent is refused, naming the parameter:

trait Sink
    put(*self, bytes: []const u8)

struct Bad
    held: []const u8

impl Sink for Bad
    put(*self, bytes: []const u8)
        self.held = bytes

var b: Bad
var s: *Sink = &b
var buf: [8]u8

s.put(buf[0..<4])
keeps what it is passed as 'bytes', but a method of 'Sink' borrows its parameters for the call

Reading from the view is not keeping it. The check distinguishes the two by what the body does, not by how it is written.

A parameter is held to the rule part by part, and a string is not a part it covers. A method may keep a string it is handed, and the call pays for that instead: a call through an object takes each string an argument holds to be kept, and moves an array one was laid over (strings.md § A string that shares a buffer). So an implementation handed a struct of a slice and a string that stores the string and only reads the slice has kept no view, and the caller’s array stays in its frame:

struct Entry
    nums: []const int
    name: string

trait Taker
    take(*self, e: Entry)

struct Last
    name: string
    total: int

impl Taker for Last
    take(*self, e: Entry)
        self.name = e.name

        for n in e.nums
            self.total += n

@no_alloc
feed(t: *Taker)
    var nums: [3]int = [1, 2, 3]

    t.take(Entry(nums[0..<3], "three"))

var last = Last("", 0)

feed(&last)
print(last.name, last.total)
three 6

Storing e.nums, or e whole, is refused as above. A struct built where it is passed is followed field by field, and so is one bound to a local that is never assigned again or lent by address: val e = Entry(nums[0..<3], "three") then t.take(e) is the same call. A local that is assigned again is a single value, and handing it on lets out everything it views.

A string read out of the elements of a slice is a copy of an element, and may be kept too. An implementation handed a []string that stores names[0] keeps what that element viewed and nothing of the caller’s array, so the array stays where it is, and the call lets out what the elements view instead:

trait Namer
    take(*self, names: []const string)

struct First
    name: string

impl Namer for First
    take(*self, names: []const string)
        self.name = names[0]

@no_alloc
feed(t: *Namer, who: string)
    var names: [2]string = [who, "second"]

    t.take(names[0..<2])

var first = First("")

feed(&first, "ada")
print(first.name)
ada

Storing names itself, or a part of it such as names[0..<1], keeps the view and is refused. An element that holds a slice of its own is held to the rule as a slice, and what is read through a pointer, a box or a second slice still counts as kept from the caller.

A raw pointer parameter is held to the rule the same way. An implementation that stores the pointer, or keeps one it derived from it (&n.field, or the field of a struct parameter that holds one), is refused at the place it lets the pointer go; one that only reads and writes through it, or hands it back, is accepted:

trait Bump
    bump(*self, n: *int)

struct Keeper
    held: *int

impl Bump for Keeper
    bump(*self, n: *int)
        self.held = n

var x = 3
var k = Keeper(&x)
var b: *Bump = &k
var y = 9

b.bump(&y)
'bump' keeps its parameter 'n' (it is stored in 'self.held'), but a method of 'Bump' borrows its parameters for the call

Because the rule is verified, a pointer a closure is lent may be passed through a trait object. A method that has to retain what it is handed takes something it can keep — a copy, a &T — rather than a view or a pointer into somebody else’s storage.

Reaching a trait’s members without a value

A trait may declare a member with no receiver, an associated function, reached through the type rather than through a value of it:

trait Origin
    origin() -> Self

impl Origin for int
    origin() -> int = 0

start[T: Origin]() -> T = T.origin()

var n: int = start()

print(n)
0

T.bits() inside a generic body, Self.bits() inside a member’s, and u32.bits() anywhere reach the same member through three ways of naming its type.

A built-in type may carry one. Through a bound the name used is the parameter, which every type has whether or not it has a name of its own, so impl Word for u32 may declare bits(), and impl Float for real declares the epsilon and the two values no literal can express. A composed type has no name at all, so an impl for []int refuses a member with no receiver.

The library’s own pair is Zero and One, the simplest use of this feature: one member each, no receiver, and the only way a generic body can write the value an accumulation starts from. They are in the standard module, so T.zero() under a [T: Add + Zero] needs no import, and a T that is itself generic brings its own arguments, so the result at T = Complex[real] is a Complex[real] with nothing written at the call.

They are also where a receiverless member is provided by the compiler rather than written. Every integer type implements both, with no block anywhere: the iN/uN families are open, so membership over them has to be a rule, and a rule about a member with no receiver has to say what it compiles to rather than reading it from a value. T.zero() at an integer is the literal, so a bounded accumulator costs no call. Expressions has the rule and the two types it does not cover.

It may carry a default, exactly as a member with a receiver may, and it is inherited and replaced by the same rules: an implementation that leaves the member out gets the trait’s body, and one that wants its own says override. This lets a trait made entirely of receiverless members grow: a member added with a default needs no edit in the implementations for which the default is right.

trait Sized
    width() -> usize
    doubled() -> usize = Self.width() * 2

struct Word
end Word

impl Sized for Word
    width() -> usize = 4

show[T: Sized]() = print(T.width(), T.doubled())

show[Word]()
4 8

The one member that may not carry a default is a static property, for its own reason: it has no parameter list and no receiver, so nothing could vary the value a default would give every implementation.

trait Maker
    static size -> int = 1
'Maker.size' is a property of the type, so a default body would give every implementation the same value — drop the body and let each one supply it

It is static dispatch only. Object safety already excludes a member with no receiver, because a table slot is selected by the receiver and there is nothing to select with. A trait declaring one is usable as a bound but not as an object, as is one that mentions Self twice.

A function can be entirely about a type without mentioning it in its signature, such as describe[T: Word]() -> string. It is called with an explicit type argument, as show[Word]() is above (generics).


Next: generics.

Search

Esc
↑↓ to navigate ↵ to open Esc to close