Generics
Type parameters, bidirectional inference, bounds checked at the definition, and monomorphization.
A type parameter list in square brackets makes a function, struct, or enum generic. A parameter name stands for a type not yet known, and may appear anywhere a type may — a parameter type, a return type, a field type, a variant payload, a local annotation.
id[T](x: T) -> T = x
struct Pair[A, B]
first: A
second: B
var p = Pair(1, "one")
print(id(7), id("hi"), p.first, p.second)
7 hi 1 one
Three further declarations take parameters without declaring a type of their own: an impl block,
whose subject is a generic type or a composed shape applied to them; a member, which may be generic
over types of its own beyond its type’s; and a trait, which is then a family of promises rather
than one. All three are on traits.
Every parameter in one list takes a name of its own, whatever declaration writes the list and
whatever kind each parameter is — a type, a value or a
pack. A member may not reuse a name its type’s block
already declares either. A function’s ordinary parameter, f[T](T: int), is in another namespace
and may share a type parameter’s name.
twice[U, U](a: U) -> U = a
print(twice(1))
'U' is already a parameter of this list
[] means type application in a type, indexing in an expression
Square brackets are reused for two things, disambiguated by position, with no new token:
- in a type,
Box[int]andResult[Box[int], string]apply type arguments to a generic type, and nesting is ordinary; - in an expression,
a[0]indexes, and never reads as a type application.
The reuse is unambiguous because a type and an expression never occupy the same grammatical slot.
Explicit type arguments at a call head are the one place the two meet: the parser reads
id[int](7) as “index id by int, then call”. The compiler resolves the head first, and a
function cannot be indexed, so the brackets are read as type arguments:
id[T](x: T) -> T = x
print(id[int](7), id[string]("s"))
7 s
The list takes () and tuple types as the type grammar does: id[()](()) instantiates at
unit, id[(int, bool)]((1, true)) at a tuple, and either may sit inside an application
(Option[()], Result[(), string]). A subscript holding parentheses still subscripts: a[(1)]
indexes, and b[()] and g[(i, j)] index a type with an Index for () or for a tuple. On such
a value the parentheses may go: g[i, j] is a subscript with two indices
(reference/arrays.md), read so wherever the
head is a value rather than a generic.
id[T](x: T) -> T = x
print(id[()](()) == (), id[Result[(), string]](Ok(())).is_ok())
true true
The nearest binding wins, as it does for every call form. A local that shadows a function’s name makes the brackets an index again, with no diagnostic about type arguments.
Writing the type arguments
The heads below take an explicit list. A value argument is written exactly as a type argument is, since the two share a list and a position:
| written | what it names |
|---|---|
id[int](7) | a function, qualified or not |
chunk[8]() | a value parameter — [const N: usize] |
Pair[int, real](1, 2.5) | a constructor, qualified or not |
x.pick[int](3) | a method — its own parameters, not the receiver’s — on any receiver, a bounded type parameter’s included |
Maybe[int].Just(1) | a variant, which is a construction of its type — qualified or not |
Box[int].of(1) | an associated function, called at the instantiation the list names — qualified or not |
va_arg[int](ap) | a special form: va_arg and ptr_cast |
A method reached through a bound takes its list the same way a method on a concrete receiver
does. The list names the trait member’s own parameters, which is the only way to supply one that
no argument mentions. A trait’s default body writes it on self in the same form:
trait Out
one[A](self) -> int
two(self) -> int = self.one[string]() + 1
struct Sink
n: int
impl Out for Sink
one[A](self) -> int = self.n
via[O: Out](o: *O) -> int = o.one[int]()
var s = Sink(41)
print(s.one[int](), via(&s), s.two())
41 41 42
An associated function called on an applied name runs at that instantiation, exactly as it does through an alias of it. For a function with no receiver, the applied name may be the only thing that says what the type’s parameters are:
struct Of[T]
x: T
twice() -> int = 2
of(x: T) -> Of[T] = Of(x)
type OfInt = Of[int]
val o = Of[u8].of(200)
print(OfInt.twice(), Of[int].twice(), Of[Of[string]].twice(), o.x + 55)
2 2 2 255
The list belongs to the type, so an instance method is refused there, since a type in front of the dot is not a value to call it on. A member read without a call is likewise told where the type is written instead:
struct Of[T]
x: T
get(self) -> T = self.x
val o = Of(3)
print(Of[int].get(o))
'get' is an instance method of 'Of' — call it on a value, not the type
A head reached through its module takes the list exactly where the bare name would, so a type from another module is instantiated without importing its name:
import sysl.math.complex
val z = complex.Complex[f32](1.5, 2.0)
print(z.re, z.im)
1.5 2
Inference supplies the arguments nearly everywhere, and a list that inference would have found
only adds noise. The explicit form is for a signature neither direction of inference reaches, such
as a kernel whose width is a value parameter that no argument mentions and whose result is unit:
add[const W: usize](a: []const f32, b: []const f32, out: []f32)
var i: usize = 0
while i + W <= a.len
val l: <W>f32 = a.load(i)
val r: <W>f32 = b.load(i)
out.store(i, l + r)
i += W
end add
val xs: [8]f32 = [1, 2, 3, 4, 5, 6, 7, 8]
val ys: [8]f32 = [10, 20, 30, 40, 50, 60, 70, 80]
var out: [8]f32
add[4](xs[..], ys[..], out[..])
print(out[0], out[7])
11 88
Without the brackets nothing says what W is, and the diagnostic states that directly:
add[const W: usize](a: []const f32, out: []f32)
val v: <W>f32 = a.load(0)
out.store(0, v)
var xs: [8]f32
var out: [8]f32
add(xs[..], out[..])
'W' is in neither the parameters of 'add' nor its result
A list may stop short: it fills the leading parameters, and the trailing ones are inferred. The parameter only a bound mentions is written, and the rest come from the arguments and the expected type, exactly as when no list is written. This holds for a function, a nested function, a method’s own parameters and an associated function’s:
trait Make
make(n: int) -> Self
impl Make for int
make(n: int) -> int = n * 10
decode_as[V: Make, S](n: int, source: S) -> V = V.make(n)
print(decode_as[int](4, "input"))
40
What the list wrote is not reconsidered, so an argument that disagrees with it is refused rather than re-solving the parameter. A longer list than the parameters is refused, and so is a trailing parameter nothing in the call can settle:
pick[A, B](a: A) -> A = a
print(pick[int](1))
'B' is in neither the parameters of 'pick' nor its result, so nothing in this call says what it should be
A type pack takes a run of the list. A type pack (..A) stands for a list of types rather
than one, so in a written list it takes every argument the other parameters leave
(below). A bare arrow’s own parameter is always inferred,
as described below.
An associated function selected from the plain name of a generic type takes its type’s
parameters from the annotation on the binding. The annotation also reaches through an unwrap() or
an expect(…) when the function returns an Option or a Result of the type it builds: the binding
says what the wrapper carries, and that settles the type. The call’s own arguments are read first,
so where they already settle the type, the binding is checked against it as usual:
struct Gain[S]
k: S
of(x: real) -> Result[Self, string]
if x > 1 then return Err("too loud")
Ok(Gain(S(x)))
end Gain
val g: Gain[q15] = Gain.of(0.5).unwrap()
val h: Gain[q31] = Gain.of(0.25).expect("in range")
print(g.k, h.k)
0.5 0.25
A bare arrow is not in the list
f: T -> U is shorthand for a bounded type parameter: the callable’s own type, which the compiler
adds and no program writes. A declaration therefore carries more parameters than were written on
it, and the explicit list covers only the written ones:
chain[A, B](x: int, f: int -> A, g: A -> B) -> B = g(f(x))
print(chain[int, int](3, n -> n * 2, n -> n + 1))
7
The two arrows add two parameters, and [int, int] still names A and B. The added parameters
cannot be written: their argument is the closure’s own anonymous type, and the compiler names them
with a character no identifier may contain.
This matters for a parameter that only a bound mentions. Inference does not reach it, so the list is the only way to supply it, and a declaration that also takes a callback relies on both rules at once:
trait Has[T]
get(self) -> T
struct Box
v: int
impl Has[int] for Box
get(self) -> int = self.v
pick[B: Has[T], T, N](b: B, f: T -> N) -> N = f(b.get())
print(pick[Box, int, int](Box(4), n -> n + 1))
5
Taking an address is the one case that cannot be resolved. &f[…] has no arguments to read the
callable’s type from and no way to write it, so a function taking a bare arrow has no address, and
the diagnostic says why:
chain[A, B](x: int, f: int -> A, g: A -> B) -> B = g(f(x))
val p = &chain[int, int]
print(1)
'chain' takes a bare arrow, so it is generic in the callable's own type — and an address has no arguments to read that from, nor any way to write it. Declare the parameter as a counted callable, '&Fn(…) -> …', to give it a type that can be named
The boxed spelling is what an address needs: &Fn(T) -> U is a type with a name,
so a function declared with one is an ordinary generic and its address is written as any other.
Every type an annotation can spell can be written in the list. The parser reads each item as an
expression first, so a subscript, T, *T, &T, Box[T] and (A, B) read as usual; where that
reading does not end the item at the , or ], it reads the item again as a type from the same
place. That admits &sync T, []T, []const T, [N]T, a nested application and a boxed callable:
import sysl.buf.{Buf, buf}
struct Shared
n: int
val shared = buf[&sync Shared]()
val views = buf[[]const u8]()
val rows = buf[[3]int]()
val nested = buf[Buf[&sync Shared]]()
print(shared.len(), views.len(), rows.len(), nested.len())
0 0 0 0
A type that is refused in an annotation is refused here with the same message, so a bare callable
int -> int gets the advice to write &Fn(int) -> int. In a subscript, such a type is reported as
not being a value:
first(xs: []const int) -> int = xs[&sync Missing]
this is a type, and a value is wanted here
The address form &f[T] names one instantiation of a generic function. Its main use is a C
callback whose signature does not mention the type parameter. See
the FFI reference.
Construction
Applying a generic type names a concrete instance: Box[int] is the type of a box of int,
Pair[int, string] a pair. Constructing one is an ordinary construction, with the type arguments
inferred from the arguments (Box(41), Pair(1, "one")) or written on the name, where they mean
what an annotation means: Pair[int, real](1, 2.5) fixes the instantiation and checks the fields
against it. The memory mode is chosen as for any construction: Box(41) is a value unless a
&Box[int] is expected.
Inference is bidirectional
Type arguments are inferred from two directions.
From the arguments. id(7) infers T = int, and inference reaches through a generic
construction, so every nested parameter is solved at once:
struct Box[T]
v: T
id[T](x: T) -> T = x
var b = id(Box(id(5)))
print(b.v)
5
From the expected type, when the arguments cannot determine a parameter. A generic function with
no parameters has nothing in its argument list to fix T, so the binding’s declared type supplies it
and the return type flows inward:
empty[T]() -> Option[T] = None
var e: Option[real] = empty()
e match
Some(v) -> print("some", v)
None -> print("none")
none
A bare result standing where a &T is wanted is the &T, whether the position is a &T
parameter or a binding declared &T, so the call hands back the reference it was given rather than
a box of a copy. An argument that settles the parameter first still wins, and where the parameter’s
own bounds refuse the &T (make[P: Zero]() -> P at a &int), it is read at the T and the
position boxes what comes back:
struct Mark
n: int
pick[P](r: &Mark) -> P
if const P::Kind == Kind.Ref
r
else
*r
bump(t: &Mark)
t.n += 1
val m: &Mark = Mark(9)
bump(pick(m))
val same: &Mark = pick(m)
print(m.n, same === m)
10 true
The expected type reaches through a form that unwraps. What ?, .unwrap(), .expect(msg),
.unwrap_or(d) and .unwrap_or_else(f) yield is the success half of an Option or Result, so
what their position wants is what that half is solved as. A match whose arm is Some(v) -> v or
Ok(v) -> v hands the payload on as it is, and says the same:
parse[T: Zero]() -> Result[T, string] = Ok(T.zero())
none[T]() -> Option[T] = None
run() -> Result[int, string]
val n: u8 = parse()?
val d: u8 = none().unwrap_or(250)
val m: u8 = none() match
Some(v) -> v
None -> 5
print(n, d, m)
Ok(0)
run().unwrap()
0 250 5
Only the success half is read. The error half is what ? propagates and .unwrap() traps on, and
nothing about unwrapping says what it is, so both[T, E]() -> Result[T, E] behind a ? still asks
for E. A ? with nothing expected (a statement, or the receiver of g()?.h()) asks for T too:
both[T: Zero, E]() -> Result[T, E] = Ok(T.zero())
run() -> Result[int, string]
val n: int = both()?
Ok(n)
cannot infer the type argument 'E' of 'both' here — annotate the expected type
An argument is read against what its parameter already knows. Ok(1) alone says nothing of its
error type, but at a parameter written Result[T, Fault] the Fault is fixed by the declaration, so
the variant takes it and its value settles T. The same holds for a closure’s result where the
parameter states one:
enum Fault
Bad
keep[T](r: Result[T, Fault]) -> Result[T, Fault] = r
run[T](f: () -> Result[T, Fault]) -> Result[T, Fault] = f()
print(keep(Ok(1)).unwrap(), run(() -> Ok("two")).unwrap())
1 two
A parameter that leaves the variant’s other half open as well (both[T, F](r: Result[T, F]) called
as both(Ok(1))) is still refused.
The model is unification: each parameter is solved by matching the declared parameter and return types against the actual argument types and the expected type. A parameter left undetermined by both directions is a compile error asking for an annotation on the binding. There is no silent default.
A literal is consulted last, because it has no type of its own to offer. A literal takes its type from where it appears, and a parameter still being solved cannot yet provide one. So an argument that already has a type settles the parameter first, then the expected type, and a literal’s default applies only where nothing else reached it:
pick[T: Add](a: T, b: T, c: T) -> T = a + b + c
print(pick(1, 2, 250u8))
253
The result is a u8 because one argument had a type and two did not, while id(7) is an int
because no argument had one. Once the parameter is solved, the literals are read against it, in the
same order the operand rule uses inside an expression.
An if or a match every arm of which is a literal counts as one, as does a block ending in one,
however they nest; an arm that leaves (continue, return, a panic) is no evidence either way. So
the expected type reaches the arms exactly as it reaches a single literal, and no suffix is needed:
import sysl.buf.{Buf, buf_filled}
val c = true
val live: Buf[u8] = buf_filled(3, if c then 255 else 0)
print(live.len(), live[0])
3 255
An arm that does not fit is refused at that arm, as a lone literal is: under Buf[u8],
if c then 256 else 0 says the literal 256 does not fit byte at the 256, and if c then 1 else 2.5
points at the 2.5, the one arm the u8 could not reach:
import sysl.buf.{Buf, buf_filled}
val c = true
val b: Buf[u8] = buf_filled(3, if c then 1 else 2.5)
'value' of 'sysl.buf.buf_filled' is byte, but real was given
A construction built only from literals is consulted last for the same reason. Some(3) is a
call rather than a literal, but the only thing that gives it the type Option[int] is the unsuffixed
3. An argument carrying a type of its own therefore outranks it, whichever end of the call it is
written at:
same[T: Eq](a: T, b: T) -> bool = a == b
var s: Option[usize] = Some(3)
print(same(s, Some(3)), same(Some(3), s), s == Some(3), Some(3) == s)
true true true true
An array literal and a generic struct follow the same rule, and it nests: Some(Some(3)) is built
only from literals at every level.
Two things stop it, and each is an explicit statement of the type. A suffix anywhere inside
fixes the width, so Some(3u8) has a type of its own, and disagreeing with it is a diagnostic. An
ordinary call is not a construction: f(3) is written exactly as Some(3) is, but its result
has nothing to do with the literal passed in, so it keeps its declared return type.
f(n: int) -> Option[int] = Some(n)
var s: Option[usize] = Some(3)
print(f(3) == s)
'==' needs matching types, got Option[int] and Option[usize]
A parameter is solved to the type that was written. Where that is a transparent subtype, the parameter carries the subtype rather than the base it is stored as, however the call states the type:
type Age = int within 0..150
widest[T]() -> T = T::Max
val written = widest[Age]()
val expected: Age = widest()
print(written, expected)
150 150
The three routes agree because they are three ways of saying the same thing. Writing Age at the
call, passing an Age as an argument, and annotating the binding Age each name the type the call
is instantiated at; a result that depended on which one was used would describe the call rather
than the type.
A transparent subtype still is its base for every value: an Age is accepted where an int is
expected, and the reverse. A type parameter is not a value, though. It is the name a body is written
against, and the body may ask it something its base would answer differently. T::Max is the only
such question.
null is not consulted at all; it waits. Unlike a literal, it has no default to fall back on.
The argument that cannot contribute is set aside, the others solve the parameter, and null is then
read against the result:
two[T](a: *T, b: *T) -> bool = a == b
var x = 3
print(two(&x, null), two(null, &x))
false false
The order of the arguments does not matter, and setting null aside cannot lose the solution,
because an argument with no type of its own has nothing to unify. Where nothing else reaches the
parameter, the call is refused rather than defaulted: inference does not invent a pointee type.
one[T](a: *T) -> bool = true
print(one(null))
'null' takes its type from its context, and there is none here
A closure waits for the same reason, and contributes once it is typed. A closure carries no type of its own, since its parameters come from the position it is written in, so it is set aside at first. Once typed, its result joins the solution, and the closures still waiting are read against it. One closure’s result can therefore be another’s parameter, regardless of which one is declared first:
forward[T](f: int -> T, g: T -> int) -> int = g(f(0))
backward[T](g: T -> int, f: int -> T) -> int = g(f(0))
print(forward(n -> n + 1, n -> n * 2), backward(n -> n * 2, n -> n + 1))
2 2
The two declarations differ only in the order of their parameters. Each round of inference reads the arguments it can, so an argument waits on what it needs rather than on its position.
A parameter that mentions no type parameter takes no part in inference, and its argument is checked against it exactly as for a non-generic function:
at[T](x: T, n: usize) -> string = n.to_string()
print(at("v", 7))
7
This matters because inference examines the arguments before it knows any types. Without this rule a
generic function would lose the rules that need an expected type: a parameter’s type fixing an
unsuffixed literal, and the coercions to &T and to a trait object. A usize parameter stays a
usize even in a generic declaration.
Members and associated functions
A method needs no inference for its type’s parameters. Its receiver already is a Box[int],
so the type’s arguments are read rather than solved.
An associated function has no receiver, which puts it in the same position as a generic free function, so it is inferred by exactly the rule above:
struct Box[T]
v: T
of(x: T) -> Box[T] = Box(x)
var b = Box.of(41)
print(b.v)
41
Self in the signature is the type applied to its own parameters, so writing -> Self and writing
-> Box[T] infer alike.
A type reached through its module infers exactly as its imported name does, a dotted path being
one name, so what receives complex.Complex.i() settles its arguments as it would Complex.i()‘s:
import sysl.math.complex
val z: complex.Complex[f32] = complex.Complex.i()
print(z.re, z.im)
0 1
A type parameter that stands for a generic type brings that type’s arguments with it, so there
is nothing to infer: when T is Box[int], T.origin() calls Box[int].origin, not Box.origin
with its argument still to be found. A generic body therefore reaches an associated function of a
generic type with no annotation, which is what makes T.zero() usable in a body
that knows neither the width nor whether the type is a Complex:
trait Origin
origin() -> Self
impl Origin for int
origin() -> int = 0
struct Box[T]
v: T
impl[T: Origin] Origin for Box[T]
origin() -> Box[T] = Box(T.origin())
seed[T: Origin](xs: []const T) -> T
var s = T.origin()
s
var bs: []Box[int] = [Box(7)]
print(seed(bs).v)
0
Self inside the type’s own body is the same case seen from inside, and needs no annotation either.
A bare type name is different: Box.origin() applies Box to no arguments, so there the
expected type alone settles them.
A member’s own type parameters are inferred the same way. The receiver supplies the type’s arguments and says nothing about the member’s, so those are solved by the rule above:
struct Pair[A, B]
first: A
second: B
struct Box[T]
v: T
with[U](self, x: U) -> Pair[T, U] = Pair(self.v, x)
var b = Box(1)
var p = b.with("x")
print(p.first, p.second)
1 x
The two lists are checked against their bounds separately, each under its own names. The names must not collide: a member may not name one of its own parameters the same as one of its type’s.
Bounds
A type parameter is bounded by a trait, and the bound states what the body of a generic may assume about the parameter.
An unbounded parameter permits only what every type supports
With no bound, T may be used only for the operations every sysl value supports. Because of the
memory model, that is a useful set: a value can be copied, assigned, passed, returned, and stored
in a struct field, an enum payload, an array, or a slice.
That set covers id[T], Box[T], Pair[A, B], and every other container: they move data around
without inspecting it, and they need no bound.
struct Box[T]
v: T
keep[T](x: T) -> Box[T] = Box(x)
var a = keep(5)
var b = keep("hi")
print(a.v, b.v)
5 hi
Every sysl value is copyable: assignment copies, and copying a value that holds a &T retains
it. There is therefore no Copy bound to write. This is simpler than Rust, where T is
move-by-default and T: Copy or T: Clone appear throughout generic signatures. In sysl, holding
and passing along any T is the unmarked baseline.
What an unbounded T may not do is anything that assumes structure: no operator, no method call,
no field access, no index. Each is a capability some types have and others lack, so each requires a
bound that guarantees it, and each is refused at the definition with the bound to write:
sum[T](a: T, b: T) -> T = a + b
print(sum(2, 3))
'+' needs 'T: Add'
A subscript is among them, because a subscript is the single method of Index, so it is checked
against the bounds exactly as a method call is.
A field is the exception. Every other unsupported use names the bound that would allow it. A field names none, because a trait describes behaviour and a field is layout, so no bound can supply one:
first[T](x: T) -> int = x.v
print(1)
'T' is a type parameter, so it has no fields to read — a field is layout, and no trait declares a property 'v' that a bound could promise instead
This is decided at the definition, not by the types that are later passed in: first[T](x: T) = x.v
is an error even if every call passes a type with a v. A generic reaches a value’s data through a
member the bound declares, which also lets two types satisfy one bound while storing the value
differently.
x.v looks like a field read but need not be one, so the diagnostic is issued only after looking for
a property of that name. A property is behaviour, a trait may declare one, and reading it through
a bound is as ordinary as calling a method.
A bound is a trait, written [T: Trait]
sum[T: Add](a: T, b: T) -> T = a + b
smaller[T: Ord](a: T, b: T) -> T = if a < b then a else b
print(sum(2, 3), smaller(9, 4))
5 4
a + b inside sum type-checks because T: Add provides the operator. Without the bound, sum
fails at its own definition, at the line that made the unsupported assumption. This is the main
advantage over the template model: the error is reported on the definition that is wrong, not on a
distant caller that instantiated it with the wrong type. Swift, Kotlin and Scala also check bounds
at the definition; C++ templates defer the check to instantiation.
Operators are available through bounds because operators are trait
methods: + is Add, < is Ord, == is Eq.
Multiple bounds are joined with +: [T: Ord + Hash] requires both. The + is unambiguous in a
bound position, since it stands between trait names rather than values.
A bound is the trait applied, so it carries the trait’s own type arguments where it has any:
| written | means |
|---|---|
[X: Sink[int]] | the body’s x.put(…) takes an int |
[X: Into[Y], Y] | and Y is solved at the call |
[X: Into[Y], Y: Display] | a body that may also print what the conversion yields |
The arguments are ordinary types, so one may name another parameter of the same declaration, and what a body may do with that parameter is what its bounds allow. Inference does not run backwards through a bound: a parameter that appears only there is solved from the result type or annotated.
A bound also makes the trait’s
associated types available, written
T::Name. They cover what the table above cannot: a trait argument is written by whoever names the
trait, but an associated type is chosen by the implementation. With [S: Seq], a body names
S::Item without the caller having to write it, and S::Item is abstract there, allowed exactly
what the trait declares for it, as S itself is.
S::Item may appear wherever a type may, including in a callable’s signature. A bare arrow is
shorthand for a bound of its own, so f: S::Item -> N is a parameter whose type is never written
out: the implementation chose it, the bound reaches it, and the closure at the call site is
monomorphized against it without being boxed.
trait Walk
type Item
step(*self) -> Option[Self::Item]
struct Down
n: int
impl Walk for Down
type Item = int
step(*self) -> Option[int]
if self.n <= 0 then return None
self.n -= 1
Some(self.n)
first_of[S: Walk, N](s: *S, f: S::Item -> N) -> Option[N] =
s.step() match
Some(t) -> Some(f(t))
None -> None
var d = Down(3)
print(first_of(&d, n -> n * 10).expect("a step"))
20
n is an int because Down declares type Item = int; first_of never mentions int.
A reference meets its referent’s bounds
A member call sees through one level of & or * ([traits]({{< ref “traits” >}})), so r[i] and
r.len() on a &Buf[int] reach the buffer. A bound agrees with it: a &T, a &sync T or a *T
meets B wherever T does, and the body, instantiated at the reference, reaches the referent’s
members exactly as the same call written on the reference would. A write through one lands in the
object the caller holds, and an associated type is the referent’s:
import sysl.buf.{Buf, buf}
read_at[C: Index[usize, int]](c: C, i: usize) -> int = c[i]
write_at[C: IndexSet[usize, int]](c: C, i: usize, v: int)
var w = c
w.index_set(i, v)
struct Up
i: int
hi: int
impl Iterate for Up
type Item = int
next(*self) -> Option[int]
if self.i >= self.hi then return None
self.i += 1
Some(self.i)
first[I: Iterate](it: I) -> Option[I::Item]
var c = it
c.next()
main()
var b: Buf[int] = buf()
b.push(1)
b.push(2)
val r: &Buf[int] = b
write_at(r, 0, 10)
write_at(&b, 1, 20)
print(read_at(b, 0), read_at(r, 1), read_at(&b, 0))
var up = Up(0, 3)
print(first(&up).unwrap(), up.i)
10 20 10
1 1
It is the traits a call on a reference reaches its referent through, and no others. A reference is refused a bound its referent meets where that would make the body do something the same call on the reference does not, and the refusal says which:
- through a
*T,EqandHash, which a pointer meets by its address ([memory]({{< ref “memory” >}})), and a trait that requires either. A&Tmeets both through its referent where the referent has them and by its identity where it does not, so a trait requiring either is met through a&Tlike any other; - through a
*T, an operator’s trait —PartialOrd,Addand the rest, andOrd, which requiresPartialOrd— since a pointer’s arithmetic is its address’s. A&Tmeets them through its referent, because an operator sees through a reference; - a member naming
Selfaway from its receiver, such asmerge(self, other: Self), sinceSelfwould be the reference in the body and the referent in the implementation; - an associated function or a static property, which is reached through the type, and a reference’s type has none of its referent’s;
- through a
*T, a member taking&self, which needs the counted box a pointer does not carry; - a second level: a
& &Buf[int]meets nothing through the buffer, asrr.len()does not reach it.
struct N
v: int
impl Ord for N
lt(self, rhs: N) -> bool = self.v < rhs.v
least[T: Ord](a: T, b: T) -> bool = a < b
main()
val n: *N = null
print(least(n, n))
'least' requires its type parameter 'T' to implement 'Ord', but *N does not — N implements it, but 'PartialOrd' is an operator's, and an operator does not see through a pointer
Display and Debug are reached through, so v.display(…) on a T at *P is P‘s. What
v.to_string() and s"${v}" render is decided by the rule for showing a value, and a raw
pointer is shown as its address there ([traits]({{< ref “traits” >}})), at T = *P as on a *P
local.
Operators see through a reference
An operator resolves through a &T. An operand held as a counted reference — a &T or a
&sync T — is read through to the T it reaches, one level, as a member call’s receiver is, and the
operator is T‘s. That holds for every binary operator, - and ~, the orderings, and the
compound assignments. A reference has no arithmetic and no order of its own, so nothing is taken
from it; == keeps its own rule, the referent’s Eq where it has one and identity where it does
not ([memory]({{< ref “memory” >}})).
- The forms mix:
&T op &T,&T op T,T op &T, a literal either side, and a&Tbeside a&sync T. A literal beside a&Tis read at whatT‘s implementations take, as beside aT. - The right operand is the one the implementation was written to take. Where
Timplements the operator at the reference —impl Add[&Big, &Big] for Big— the right operand stays a reference; otherwise it is read through as well. r op= von a&Tplace isr = r op v: the result is stored into the place, aTboxed into it or the&Tthe implementation gives back. What the place held before is not written, so another reference to it keeps its value. The place is read twice, so it is a name, or fields and subscripts of one by a name or a literal; a place with a call in it is refused, as is a write of several places at once.- A bound on an operator’s trait is met through a
&T:[T: Add]at&Vis met byV‘sAdd, whoseRhs = SelfisVon both sides of the body’sa + b, and a member of the trait named in the body (a.lt(b)) reads through the same way. Through a*Tit is not: a pointer’s arithmetic is its address’s. - One level: a
& &Treads through to the&T, which has no operators.
So a type with a Drop, held only as a &T, has operators. Its implementations take self —
the copy a method is handed — and name &Big where they take or give another value:
struct Big
n: int
impl Drop for Big
drop(self) = print("free", self.n)
impl Add[&Big, &Big] for Big
add(self, rhs: &Big) -> &Big = Big(self.n + rhs.n)
impl Add[int, &Big] for Big
add(self, rhs: int) -> &Big = Big(self.n + rhs)
impl Ord for Big
lt(self, rhs: Big) -> bool = self.n < rhs.n
total[T: Add](a: T, b: T) -> T = a + b
run()
val a = Big(1)
val b = Big(2)
var acc = a + b
acc += 10
print(acc.n, a < b, total(a, b).n)
run()
free 3
13 true 3
free 3
free 13
free 2
free 1
A referent with no implementation is the one the refusal names:
struct W
n: int
main()
val a: &W = W(1)
print((a + a).n)
operator '+' is not defined for W
A trait object satisfies a bound on the trait it dispatches through
A bound asks whether the members it names can be called on the value. A *Trait or a &Trait does
not know which type it holds, but it carries a table of exactly those members, so it satisfies
the bound, and one generic function accepts both a concrete type and an object erased from it:
trait Shape
area(self) -> int
struct Rect
w: int
h: int
impl Shape for Rect
area(self) -> int = self.w * self.h
total[T: Shape](x: T) -> int = x.area()
var o: &Shape = Rect(3, 4)
print(total(Rect(3, 4)))
print(total(o))
12
12
Monomorphization works unchanged. An object type is a concrete type, a pair
of words, so the body is instantiated at &Shape exactly as it is at Rect, once each. The two
instantiations differ in what x.area() compiles to: a direct call in one, an indirect call through
the table in the other. That difference is decided as for any two instantiations, by the type the
parameter is bound to.
This works because of object safety. A trait with a member that cannot be dispatched, such as
one that mentions Self outside its receiver, has no object type at all, rather than an object
missing that member. So the existence of a &Shape guarantees that every member of Shape is
reachable through it.
The one exception is a member that declares type parameters of its own, which is left out of the
table rather than out of the object (traits). A bound requires
every member, and an object reaches only what its table holds, so an object does not satisfy a bound
on such a trait. The diagnostic names the reason, since otherwise it would read as a &Applies
failing to implement Applies:
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)
total[T: Applies](x: T) -> int = x.tag()
val o: &Applies = N(3)
print(total(o))
a bound promises every member and an erased value reaches only what its table holds
The concrete value is unaffected: N(3).apply(…) is an ordinary call, and a bound on a type
parameter reaches it at every instantiation. Only the erased form lacks the member.
Required traits follow without a rule of their own. trait Shape: Display puts the slots of
Display in the object’s table, and a bound on a trait is satisfied wherever a bound on a trait that
requires it is, so a show[T: Display](x: T) accepts the same o.
An object does not satisfy a bound on any other trait: the table answers the bound, and a trait with no slots in it cannot.
trait Shape
area(self) -> int
trait Weighed
weight(self) -> int
struct Rect
w: int
h: int
impl Shape for Rect
area(self) -> int = self.w * self.h
heft[T: Weighed](x: T) -> int = x.weight()
var o: &Shape = Rect(3, 4)
print(heft(o))
'heft' requires its type parameter 'T' to implement 'Weighed', but &Shape does not
Satisfying a bound is not the same as being erasable again. A bound asks what may be called
through a value; forming an object asks what may be assembled from its type, and a table is built
from a type’s implementations. An object has none, so a &Shape cannot be converted to a &Display
even though the slots of Display are inside its table: a run of slots in one table is not a table
of its own ([traits]({{< ref “traits” >}})).
A trait object stands in for a parameter behind a mode
A parameter written p: *P or p: &P asks for a pointer to some type implementing the bound. An
object is exactly that, a pointer to a type nobody remembers, so handed a *Port, P binds to the
trait Port itself and *P is the object. Every call through p dispatches through its table:
trait Port
mask(self) -> u32
struct Board
level: u32
impl Port for Board
mask(self) -> u32 = self.level
send[P: Port](p: *P, v: u32) -> u32 = p.mask() + v
var b = Board(40)
val port: *Port = &b
print(send(port, 1))
print(send[Port](port, 2))
print(send(&b, 3))
41
42
43
The first two calls are one instantiation, at Port, whatever type the object was made from; the
third is the ordinary one, at Board. An object of a trait that requires Port stands in the same
way, and P can be handed on to another generic, inferred or written as inner[P](p).
A trait has no layout, so the body may only ever reach P behind * or &. A P by value, as a
parameter, a local or a result, sizeof(P), [4]P, Buf[P], P(…), a static member and an
associated type P::Item all need the type rather than an object of it, and the call says which line
of the body asked for one:
trait Port
mask(self) -> u32
struct Board
level: u32
impl Port for Board
mask(self) -> u32 = self.level
copy[P: Port](p: *P) -> u32
val held: P = *p
held.mask()
var b = Board(40)
val port: *Port = &b
print(copy(port))
'copy' uses 'P' by value at line 11, so a trait object cannot stand in for it
An associated type is refused rather than read off the object. The object’s type fixes it, but a
body written against P::Item reads it as a type to lay out, which is the same need. The parameter
must be bounded: the calls go through the table of the trait a bound names, so a [P](p: *P)
handed an object is refused rather than bound to a trait it asks nothing of. A generic type inferred
at an object, such as Wrap(port) for a struct Wrap[P: Port] holding p: *P, follows the same rule,
and a field holding P by value is refused where it is declared.
A generic type applied to the parameter is a use behind a mode exactly when the type reaches it only
behind one. Wrap[P] lays out a pointer to the object, so it may be a parameter’s type, a result, a
local or another generic type’s field:
trait Port
mask(self) -> u32
struct Board
level: u32
impl Port for Board
mask(self) -> u32 = self.level
struct Wrap[P: Port]
p: *P
wrap[P: Port](p: *P) -> Wrap[P] = Wrap(p)
send[P: Port](p: *P, v: u32) -> u32
val w = wrap(p)
w.p.mask() + v
var b = Board(40)
val port: *Port = &b
print(send(port, 2))
42
A type that holds the parameter by value somewhere along its fields — directly, in an array, or in another generic type that does — needs its layout even behind a pointer, and the refusal names the fields that hold it:
trait Port
mask(self) -> u32
struct Board
level: u32
impl Port for Board
mask(self) -> u32 = self.level
struct Cell[T]
v: T
struct Shelf[P: Port]
cell: Cell[P]
peek[P: Port](p: *P) -> u32
val s: *Shelf[P] = null
p.mask()
var b = Board(40)
val port: *Port = &b
print(peek(port))
'peek' uses 'P' in 'Shelf[P]' (its field 'cell.v' holds 'P') at line 17
A type’s own parameters carry bounds too
The same bracketed list means the same thing on a struct or an enum, and it is where the type states what it assumes:
struct SortedPair[T: Ord]
lo: T
hi: T
ordered(self) -> bool = self.lo < self.hi
var s = SortedPair(1, 2)
print(s.ordered())
true
A bound on a type has two effects.
Every application of the type must satisfy it, wherever the application is written: a declared parameter, a result, a field of another type, a variant’s payload, a construction:
struct SortedPair[T: Ord]
lo: T
hi: T
struct P
v: int
var s = SortedPair(P(1), P(2))
print(1)
'SortedPair' requires its type parameter 'T' to implement 'Ord', but P does not
Where the argument is itself a type parameter, its own bounds decide, so a function taking a
SortedPair[U] must bound U by at least what SortedPair requires. A bound is satisfied by a
bound one level out.
The type’s members may assume the bound, so they are checked at their definition. A member of a generic type is checked once, with the parameters standing for themselves, by the same pass that checks a bounded generic function. A method calling something no bound allows is reported on its own line whether or not anything instantiates the type. A generic type’s fields are laid out once the same way, which catches a field that applies another bounded type to this one’s parameter.
A bound is declared once, at the type, and is in force everywhere its parameters appear: a
member’s signature and body, a field’s type, a variant’s payload. It is not restated per member, as
in Swift and Kotlin; Rust requires it to be repeated on each impl.
A bound may be written out of line, with where
A declaration with several parameters and several bounds pushes its parameter list, its result and
its body to the right, and the most important part, what the function takes, becomes harder to
read. where puts the bounds after the signature instead:
show[T](x: T) -> string where T: Display = x.to_string()
print(show(42))
42
It is the same bound in a different place. A clause is folded into the bracketed list, so
show[T: Display](x: T) and the form above are one declaration written two ways, with the same
inference, diagnostics and monomorphization. The choice is about layout, not meaning.
The two may be mixed, so the choice is made per bound: a short constraint stays in the brackets, and a long one that would crowd the signature goes after it.
same[T: Eq](a: T, b: T) -> string where T: Display =
if a == b then a.to_string() else "different"
print(same(3, 3))
print(same(3, 4))
3
different
The clause is a comma-separated list, and each entry joins its traits with + exactly as a bracketed
bound does. It goes at the end of the header line, after the result type and before the = or the
indented block, and it is written the same way on a function, a member, a struct, an enum, a trait
and an impl:
struct SortedPair[T] where T: Ord
lo: T
hi: T
ordered(self) -> bool = self.lo < self.hi
var s = SortedPair(1, 2)
print(s.ordered())
true
A clause may only bound a parameter the declaration actually declares, and any other name is refused where it is written:
show[T](x: T) -> string where U: Display = x.to_string()
print(show(1))
this declaration has no type parameter 'U', so a 'where' clause has nothing to bound — it takes 'T'
A value parameter cannot be bounded. [const N: usize] already states a type, and that type is
what the argument must be, not a statement about what it implements. A value implements no trait,
so there is nothing for a clause to add:
sum[const N: usize](xs: [N]int) -> int where N: Display = 0
print(sum([1, 2]))
'N' stands for a value rather than a type, and a value implements no trait — its 'const' declaration is where the type its argument must have is written
where is not a reserved word. It also introduces a
constrained type’s predicate in a type declaration,
where it follows a type rather than a signature, and it is an ordinary name everywhere else, so a
program may still name something where:
type Even = new int where value % 2 == 0
where(n: int) -> int = n + 1
var e = Even(4)
print(int(e), where(1))
4 2
A parameter may carry a default
A parameter of a trait, a struct, or an enum may name the type to use when a use leaves it out:
struct Pair[A, B = A]
x: A
y: B
total(p: Pair[int]) -> int = p.x + p.y
print(total(Pair(1, 2)))
3
The bound comes first and the default last ([R: Show = Self]), and either may be written
without the other. Four rules govern defaults:
- Defaults are filled left to right, each resolved under the arguments already fixed, so a default may name a parameter written before it; naming one written after it is a forward reference.
- They form a suffix. A parameter with no default may not follow one that has a default, because arguments are written in order and a use cannot omit an earlier one while supplying a later one.
- Defaults are filled before instantiations are identified, so
Pair[int]andPair[int, int]are one instantiation, not two with the same fields. - A default is exposed like a field. A public declaration may not default to a type less visible than itself, or a caller who leaves the argument out would hold a value whose type they cannot name. A default also may not lead back to the declaration it belongs to, directly or through another declaration’s default.
A value parameter may carry a default too. It stands for the argument a use would have written, so the same four rules govern it: it fills the same gap, counts toward the same arity and sits in the same suffix. A type default and a value default may share one list, and a value default may be a constant expression over the value parameters written before it:
struct Ring[T, const N: usize = 4]
items: [N]T
struct Pad[const M: usize, const N: usize = M * 2]
a: [M]int
b: [N]int
val r: Ring[int] = Ring([1, 2, 3, 4])
val p: Pad[2] = Pad([1, 2], [3, 4, 5, 6])
print(r.items.len, p.b.len)
4 4
Ring[int] is Ring[int, 4], a single instantiation. The default has to fit the parameter’s
declared type, exactly as a written argument does: [const N: u8 = 300] is refused with “the
default for ‘N’ does not fit byte: 300”.
Self is the main use of a default. In a trait’s default it means the implementing type, exactly
as in a method’s signature, so impl Scale for P means impl Scale[P] for P, and [T: Scale] asks
for Scale[T]. A struct and an enum have no implementing type, so Self in one of their defaults is
refused. A trait object has none either: an object does not know which type it holds, so a
default of Self has nothing to name, and the argument must be written out.
Only those three declarations may carry defaults. The type parameters of a function, a method and
an impl block are solved from what they are given rather than written where they are used, so
there is no argument list with a gap for a default to fill — a list that stops short leaves the rest
to inference, not to a default:
f[T = int](x: T) -> T = x
print(f(1))
'T' is a type parameter of the function 'f', whose type parameters are solved from what it is given rather than written where it is used — so '= int' has nothing to stand in for
A fallback for an inference that found nothing would be a different feature, so f[T = int](x: T)
is refused rather than given that meaning.
A value parameter of a function is solved from an array’s length exactly as a type parameter is from an argument’s type, so its default is refused in the same words:
total[const N: usize = 3](xs: [N]int) -> usize = N
print(total([1, 2]))
'N' is a value parameter of the function 'total', whose type parameters are solved from what it is given rather than written where it is used — so '= 3' has nothing to stand in for
Converting through a parameter
A conversion is the one capability no bound provides, because a conversion is between concrete scalar kinds rather than something a trait declares. Both directions can be written, and both are checked at each instantiation rather than at the definition:
low[T](x: T) -> u8 = u8(x)
make[T](b: u8) -> T = T(b)
var n: int = make(7)
print(low(65), n)
65 7
T(b) is a conversion written with the parameter’s name, resolved at each instantiation, so both
directions follow one rule. An instantiation at a constrained subtype or a simple enum uses
that type’s own checked cast, since the scalar conversion has no meaning for either: T(x) at an
Age is Age(x), including its trap.
Construction is deliberately excluded. A struct’s positional constructor takes a field list rather than a value, and a generic body should not fill in an unknown struct’s fields by position by accident:
struct P
v: int
make[T](b: u8) -> T = T(b)
var p: P = make(7)
print(p.v)
cannot convert byte to P
T(b) stays a conversion at every instantiation, so at a struct it is refused exactly as u8(x)
is, naming the struct, rather than becoming a constructor. A container that needs to build a T
uses a bound that provides one.
The parameter takes precedence over a declaration of the same name, consistently: var y: T
inside a [T] body means the parameter, so T(x) on the next line means it too.
Monomorphization
Each distinct set of type arguments produces its own specialized function or aggregate. id
called at int and at real emits two functions; a Box[int] and a Box[string] are two distinct
layouts. The IR carries exactly one definition per instantiation, not one per call.
This is why bounds can be checked once at the definition yet compile to direct, monomorphic code with no dictionary passed at run time.
- The cost is code size, the standard monomorphization tradeoff, which C++ templates and Rust generics also make. It suits a systems language, where a direct, inlinable call matters, and dynamic dispatch is available when a single copy is preferable.
- Recursion is fine. A recursive generic function recurses at a fixed instantiation, so it monomorphizes like any other.
- A recursion that asks for itself at a new type at every level is refused. Its instantiations would never end, whatever value stops it at run time, so one chain of calls may hold at most 64 instantiations of one function at types that do not shrink (a recursion down a type the program wrote is bounded by it, and not counted), and an instantiation’s name at most 32768 bytes, which a type that doubles at every level reaches first. The same holds of a by-name parameter forwarded as a new expression, each level’s thunk being a new callable; forwarding the parameter itself is fine.
nest[T](n: int, x: T) -> int = if n == 0 then 0 else 1 + nest(n - 1, (x, x))
print(nest(3, 1))
the call here asks for 'nest' at a new type at every level
A nested function may be generic
A function declared inside a body takes type parameters, bounds and bare-arrow parameters exactly as a top-level one does, and each call instantiates it the same way. Each instantiation is another function over the environment its block shares, so each reads and writes the body’s variables as any nested function does.
tally() -> int
var total = 0
noted[T: Display](x: T, n: int) -> string
total += n
s"$x"
print(noted("a", 2), noted(true, 3), noted(1.5, 4))
total
print(tally())
a true 1.5
9
Two bodies may each declare a nested generic with the same name: an instantiation is identified by
the frame it belongs to as well as by its type arguments, so the two are never one function. A type
argument the call cannot settle is written out, sized[u32](), and a bound is checked at the call:
struct Point
x: int
describe() -> string
shown[T: Display](x: T) -> string = s"<$x>"
shown(Point(1))
'shown' requires its type parameter 'T' to implement 'Display', but Point does not
Variance does not arise
sysl has no variance rules. Variance decides when G[A] may stand in for G[B], which only matters
with a subtyping relation among concrete types, and sysl has none. There is no inheritance, and a
trait bound is a constraint rather than a supertype relation between values.
trait Sensor
unit(self) -> string
struct Thermometer
channel: int
impl Sensor for Thermometer
unit(self) -> string = "degC"
struct Box[T]
v: T
label(b: Box[&Sensor]) -> string = b.v.unit()
var t = Box(Thermometer(2))
print(label(t))
'b' of 'label' is Box[&Sensor], but Box[Thermometer] was given
Box[Thermometer] and Box[&Sensor] are unrelated types. This removes a whole category of design
difficulty found in languages with nominal subtyping: polymorphism over a set of types is expressed
by a bound or a trait object, never by a covariant container.
A parameter may stand for a value
A type parameter stands for a type. A parameter written const stands for a value, which
lets one declaration cover every array length:
total[const N: usize](xs: [N]int) -> int
var t = 0
for i in 0..<N do t = t + xs[i]
t
var a = [1, 2, 3]
var b = [10, 20, 30, 40, 50]
print(total(a))
print(total(b))
6
150
N is inferred from the argument exactly as a type parameter is: matching [3]int against [N]int
binds N to 3, the way matching Box[int] against Box[T] binds T. Inside the body N is an
ordinary usize: it can be used as a loop bound, computed with, and passed on.
total at N = 3 and at N = 5 are two functions, just as id at int and at real are two: the
value is part of what identifies the instantiation, so the length is a constant inside each copy.
It is the same const as everywhere else
A constant is declared const NAME: Type = expr. A value parameter is that declaration with the
initializer left to the caller, so [const N: usize] is existing grammar in a new position.
The const marker is required. [N: usize] on its own has the same shape as a bounded type
parameter such as [T: Ord], and only name resolution could tell them apart, by asking whether the
name after the colon is a trait or a type. A misspelled trait name that happened to name a type
would then silently change the kind of parameter.
Which values
Integers, bool, char, and a simple enum’s variants. A value parameter puts a value into a
type’s identity, so the compiler must decide when two values are the same and must encode each
into a mangled name. Each of these kinds supports both.
A type declares its value parameters the same way, and its arguments are written out rather than inferred, because a type has no call to infer them from:
struct Buf[const N: usize]
data: [N]byte
struct Flag[const B: bool]
v: int
var buf: Buf[4] = Buf([1, 2, 3, 4])
var on: Flag[true] = Flag(1)
print(buf.data.len, on.v)
4 1
Buf[2] and Buf[4] are two types with two layouts, and neither is accepted where the other is
expected.
An argument may be any constant expression (a const, arithmetic over one, an if or match
over one, or a call evaluated at compile time), in a type and in a call alike
(declarations.md § Calls where a type is read):
struct Ring[const N: usize]
data: [N]byte
double(n: usize) -> usize = n * 2
const Base: usize = 2
var r: Ring[double(Base) + 1] = Ring([0; 5])
print(r.data.len)
5
An associated function’s own parameters are written the same way, through the type:
struct Sq
n: u32
impl Sq
scaled[U: Integer, const K: u32](x: U) -> U = x * U(K)
const Two: u32 = 2
print(Sq.scaled[i64, Two](5))
10
An integer constant is read at the parameter’s type wherever it is handed to the parameter itself,
and refused only where its value does not fit, so const Base: int = 2 still makes Ring[Base] and
Ring[Base + 1]. Inside the expression the ordinary rules hold: a function’s parameter takes what
any call to it takes, and an int is not converted to a usize implicitly. That argument is refused
at the part that does not read, as the same call would be anywhere else — where an
untyped constant, const Base = 2, would be the
literal 2 and read as a usize:
struct Ring[const N: usize]
data: [N]byte
double(n: usize) -> usize = n * 2
const Base: int = 2
var r: Ring[double(Base) + 1] = Ring([0; 5])
print(r.data.len)
'n' of 'double' is usize, but int was given
An argument is of its parameter’s type. An integer of any width is read at an integer parameter,
and nothing else stands in for another kind: true is not 1, 65 is not 'A', and 1 is not an
enum’s second variant. An argument of another type is refused as a call’s argument is, naming the
parameter:
struct Flag[const B: bool]
v: int
var on: Flag[1] = Flag(1)
print(on.v)
'B' of 'Flag' is bool, but int was given
A member reads the parameter as an ordinary value of its declared type, and each length gets its own body:
struct Buf[const N: usize]
used: usize
room(self) -> usize = N - self.used
var a: Buf[8] = Buf(3)
var b: Buf[2] = Buf(1)
print(a.room())
print(b.room())
5
1
A simple enum, one whose variants carry no payload, gives a parameter that takes one of its
variants. The parameter’s declared type names the enum, so the variant is written bare, as it is
wherever an expected type names the enum; Level.High names the same value:
enum Level
Low
High
struct Gauge[const L: Level = Low]
reading: int
alarm(self) -> bool = L == High
var quiet: Gauge = Gauge(3)
var loud: Gauge[High] = Gauge(9)
var same: Gauge[Level.High] = loud
print(quiet.alarm(), loud.alarm(), same.reading)
false true 9
Gauge[Low] and Gauge[High] are two types, and the default makes a bare Gauge the first. A
variant of any other enum is not a value of this one, bare or qualified:
enum Level
Low
High
enum Tide
Low
Slack
struct Gauge[const L: Level]
reading: int
var g: Gauge[Slack] = Gauge(1)
print(g.reading)
'Slack' is not a variant of Level
An enum whose variants carry values cannot be the type of a value parameter, since its value is more than a tag, and neither can a float or a string:
enum Shape
Dot
Circle(r: int)
struct Holder[const S: Shape]
n: int
print(1)
'S' is a value parameter, and Shape cannot be the type of one
Floats are excluded because NaN != NaN under the ordinary comparison, which would make a type
unequal to itself. Strings are excluded because two spellings of the same text are not guaranteed
to be one value.
What may be written with N
N may be used as an array’s length, and a body may compute with it freely. A computed result may
not be carried into a signature — an array’s length, a vector’s lane count, or a type’s value
argument:
f[const N: usize](xs: [N]int) -> [N + 1]int = xs
print(1)
the length 'N + 1' computes with the value parameter 'N'
struct Buf[const N: usize]
used: usize
grow[const N: usize](b: Buf[N]) -> Buf[N + 1] = Buf(b.used)
print(1)
the value argument 'N + 1' computes with the value parameter 'N'
A signature’s types are what a call is solved from, and solving N out of Buf[N + 1] is type-level
arithmetic, which sysl does not provide.
Inside its own declaration a value parameter is a symbol. Buf[N] and Buf[M] are two types
there, just as Box[T] and Box[U] are, and a struct’s value argument computed in a field or a body
— Buf[N + 1] — is that computation over the symbols. Two such arguments are one type when they are
the same polynomial: N + M is M + N, 2 * N is N + N, N * (M + 1) is N * M + N, and a
division by a constant that divides every coefficient is exact ((2 * N + 4) / 2 is N + 2). A
comparison whose two sides differ by a constant is that constant’s truth (Flag[N + 1 > N] is
Flag[true]), and a call to a const function is the same call however it is named (double(N)
and util.double(N)). Any other operation over a parameter (N / 2 alone, !B, N > M) is
compared as written. A form too large to compare — over 64 terms, or of degree over 8 — is refused
where it is written:
struct Buf[const N: usize]
used: usize
deep[const N: usize]() -> usize
val x: Buf[N * N * N * N * N * N * N * N * N] = Buf(1)
x.used
print(1)
the type argument 'N * N * N * N * N * N * N * N * N' is too large to compare (of degree over 8)
So a body that yields what its signature does not name is wrong for every instantiation, and it is refused at the declaration, whether or not anything calls it:
struct Buf[const N: usize]
used: usize
wrong[const N: usize](b: Buf[N]) -> Buf[N] = Buf[N + 1](b.used)
print(1)
function 'wrong' should return Buf[N], but its body yields Buf[N + 1] — the two differ by 1 for every N
struct Buf[const N: usize]
used: usize
cast[const N: usize, const M: usize](b: Buf[N]) -> Buf[M] = b
print(1)
function 'cast' should return Buf[M], but its body yields Buf[N] — 'N' and 'M' are different value parameters
Where what parts the two is an operation compared as written, the refusal names it — the two may be one at every instantiation, but the form cannot show it:
struct Buf[const N: usize]
used: usize
half[const N: usize](b: Buf[N]) -> Buf[N] = Buf[N / 2 * 2](b.used)
print(1)
function 'half' should return Buf[N], but its body yields Buf[N / 2 * 2] — '/' by 2 is compared as written, and 'N / 2 * 2' is not 'N' for every N
The same holds where a local’s declared type, an argument or a field meets such a value, and a
parameter is never the constant one instantiation might choose: -> Buf[3] = b is refused with
“’N’ stands for every value an instantiation chooses, not only 3”. Forms that agree are accepted,
and each instantiation computes its own:
struct Buf[const N: usize]
used: usize
room(self) -> usize = N - self.used
struct Pair[const N: usize, const M: usize]
inner: Buf[N + M]
join[const N: usize, const M: usize](b: Buf[N]) -> Pair[N, M] = Pair(Buf[M + N](b.used))
print(join[2, 3](Buf[2](1)).inner.room())
4
An array’s length and a vector’s lane count are symbols there too. [N]int and [M]int are two
types inside the declaration, and neither is [3]int, so a result, a local’s declared type, an
argument or a field that meets the wrong one is refused at the declaration, called or not:
cast[const N: usize, const M: usize](xs: [N]int) -> [M]int = xs
print(1)
function 'cast' should return [M]int, but its body yields [N]int — 'N' and 'M' are different value parameters
f[const N: usize]() -> usize
val ys: [N]int = [1, 2, 3]
ys.len
print(1)
cannot initialize 'ys': declared [N]int but the value is [3]int — 'N' stands for every value an instantiation chooses, not only 3
A vector’s lane count is held the same way (<N>f32 is not <M>f32, and a four-lane literal does
not fill <N>f32). What carries the length carries the symbol: a repeat [x; N] is an [N]T, a
lane-wise comparison of two <W>f32 is a <W>bool, and a generic called with an [N]T solves its
own parameter to N, so its result is an [N]T again:
same[const M: usize](xs: [M]int) -> [M]int = xs
fill[const N: usize](x: int) -> [N]int = same([x; N])
print(fill[3](7)[2])
7
A length the reading cannot follow — one a shuffle or a lookup makes, or a written [0]T —
stays a number there, and each instantiation answers for it.
A type parameter held in a type is a symbol in the same way. A Box[T] where a Box[U] was
declared is refused at the declaration, in every position above, and a call at [int, int] — where
the two happen to be one type — does not make the body right:
struct Box[T]
v: T
cast[T, U](b: Box[T]) -> Box[U] = b
print(cast[int, int](Box(1)).v)
function 'cast' should return Box[U], but its body yields Box[T] — 'T' and 'U' are different type parameters
A bare T where U was asked for is left to each instantiation, because one may convert the
other: widen[T: Integer, U: Integer](x: T) -> U = x widens at [u8, int] and is refused at
[int, u8]. Nothing converts a Box[u8], an Option[u8], a [2]u8 or a &u8 into its int
twin, which is why under a type the two must be one. A parameter against a concrete type
(-> Box[int] = b) and an associated type a bound fixes (S::Error with S: Source[Error = E])
are also answered by the instantiations, which know what they are.
Writing a type parameter where a length belongs is refused for the opposite reason, since a length is a value:
f[T](xs: [T]int) -> usize = 0
print(1)
'T' is a type parameter, and an array's length is a value rather than a type
What it is for: a fixed array renders
impl[const N: usize, T: Display] Display for [N]T is one block covering every array, which is why
print accepts one:
var a = [1, 2, 3]
var b = ["x", "y"]
print(a)
print(b)
[1, 2, 3]
[x, y]
Value parameters are what let a library implement a trait for arrays of every length: [2]T and
[3]T are different types, and [N]T is generic over the difference.
An array has two shapes an impl may match, the length written out and the length as a parameter,
and the written-out one is more specific, so it is found first. A block for [2]T takes precedence
over a block for [N]T on arrays of two, the same “written out before a parameter” ordering that
override uses.
A block may instead keep the element written out and leave the length as its one parameter:
impl[const N: usize] Checksum for [N]u8 covers every array of u8, whatever its length, and N
is a value in its body as anywhere else:
trait Checksum
checksum(self) -> int
impl[const N: usize] Checksum for [N]u8
checksum(self) -> int
var total = 0
for b in self
total += int(b)
total * int(N)
sum_of[T: Checksum](v: T) -> int = v.checksum()
val header: [4]u8 = [1, 2, 3, 4]
val empty: [0]u8 = []
print(header.checksum(), sum_of(header), empty.checksum())
40 40 0
So an array has three shapes, found most specific first after the array written out in full:
every array of one length, [2]T; every array of one element, [N]u8; and every array, [N]T.
The first two each drop one argument, and a [2]u8 is covered by both with neither more specific
than the other, so one trait may have blocks of one kind or the other, not both:
trait Tag
tag(self) -> string
impl[T] Tag for [2]T
tag(self) -> string = "pair"
impl[const N: usize] Tag for [N]u8
tag(self) -> string = "bytes"
'[2]T' and '[N]u8' would both implement 'Tag' for '[2]u8', with neither more specific than the other — a trait's blocks over arrays may fix the length or fix the element type, not some of each
An array written out in full beside either of those two shapes is an overlap, and the written-out
block says override, as for any other shape.
An element mentioning another of the block’s parameters is the every-array shape, where the element
must be a parameter of its own.
A vector’s lane count is a value parameter on the same terms, so impl[const N: usize] LaneMath for <N>f32 is one block covering every vector of f32, and <N>T every vector. A vector keeps its
lane written out where an array keeps its length: <N>f32 is found before <N>T, and <4>T is
not a shape at all. Vectors has the rules.
A member declares its own
A method’s parameter list is its own, exactly as a function’s is, so it may take a value parameter the type’s parameters know nothing about. The type’s parameters are fixed by the receiver; the member’s are solved at the call:
struct Sum
seen: usize
take[const N: usize](*self, xs: [N]int) -> usize
self.seen = self.seen + N
N
end Sum
var s = Sum(0)
print(s.take([1, 2, 3]))
print(s.take([4, 5]))
print(s.seen)
3
2
5
Both calls reach one written method and neither passes a length, because N is read from the
argument as it is for a free function.
A trait declares one too
A trait’s parameter list is like a struct’s, so it may take a value. The members read it in their
signatures and default bodies, and an impl fixes it, the way impl Mul[int] fixes a type:
trait Bytes[const N: usize]
bytes(self) -> [N]u8
size(self) -> usize = N
struct W
x: u8
impl Bytes[4] for W
bytes(self) -> [4]u8 = [self.x, 0, 0, 0]
val w = W(7)
print(w.size())
print(w.bytes()[0])
print(w.bytes().len)
4
7
4
A bound names it at a value, f[T: Bytes[4]], and so does a trait object, &Bytes[4]. Two values
give two implementations, exactly as two type arguments do, so one type may implement Bytes[2] and
Bytes[4] side by side. A generic block may abstract over the value too:
impl[const M: usize] Bytes[M] for Bits[M].
A default follows the struct rules (§ A parameter may carry a default): a block or an object that leaves the argument out takes it.
trait Hash[const N: usize = 32]
digest_len(self) -> usize = N
struct Sha
x: int
impl Hash for Sha
val o: &Hash = Sha(0)
print(o.digest_len())
32
A value is checked against the parameter’s type wherever it is written, the default included:
trait Small[const N: u8]
n(self) -> u8 = N
struct S
x: int
impl Small[300] for S
this argument does not fit byte: 300
A parameter may stand for a list of types
A type parameter stands for one type and a const parameter for one value. A parameter written ..
stands for a list of types, a pack, which lets one declaration cover every tuple:
joined[..A: Display](t: (..A)) -> string
var s = ""
for const i in 0..<A.len
if i > 0 then s = s + "-"
s = s + t.i.to_string()
s
print(joined((1, 2)))
print(joined((1, "two", true, 4.5)))
1-2
1-two-true-4.5
The example uses three constructs.
..A declares the pack, and (..A) is its tuple, the only way to write a pack wherever it is
declared. It matches a tuple of any arity, and the arity is inferred from the argument exactly as an
array’s length is: a (int, string, bool) matched against (..A) binds A to those three types.
The bound applies to every member. [..A: Display] says every type in A implements Display;
it is the ordinary [T: Display] read over a list, and a pack needs no other bound syntax. Because
the bound is known before any body is compiled, a tuple holding something that cannot be displayed
is refused where it is written:
struct Point
x: int
y: int
joined[..A: Display](t: (..A)) -> string = t.0.to_string()
print(joined((1, Point(2, 3))))
Display
for const is unrolled. Its range must be known at compile time (A.len is the number of types
in the pack), and the body is repeated once per value, with i substituted as a compile-time
usize. Each copy is type-checked separately. The parts of a tuple have different types, so
t.i is a different selection in each copy, and one written line covers all of them.
t.i at a compile-time i is the same selection as t.0, with the position given as a constant
rather than a literal. It reaches the parts of a tuple and nothing else: a struct’s fields have
names, and a number does not address one.
A for const over a type’s own make-up
The subject may also be a descriptor list: for const f in T::Fields unrolls once per field of a
struct, tuple or union, and for const c in T::Variants once per variant of an enum. f and c are
compile-time descriptors, v.(f) is that field of a value in each copy, f::Type its type, and
c::Fields the payload’s own list, which a nested for const walks. Each copy is checked separately,
as a tuple’s t.i is, so one body serves fields of every type. The descriptors, and a derivation
written with them, are on reflection.
A walk of a type’s own parts unrolls once per part, with no limit: the type’s declaration bounds it, visibly, and a derivation over a hundred-variant enum emits what a hundred hand-written arms would. The limit described below applies only to a range.
What a for const will not do
A range computed at run time cannot be unrolled; the ordinary for walks one:
f(n: usize)
for const i in 0..<n
print(i)
f(3)
must be known at compile time
A range is unrolled to at most 65 copies (its two ends at most 64 apart). for const i in 0..<100000 would be one line producing a hundred thousand copies of its body, with nothing in the
source to show it. A loop that long belongs in a run-time for, which costs one copy however far it
counts:
f()
for const i in 0..<100
print(i)
f()
a 'for const' over 100 values would emit that many copies of its body, and 64 is the most one is unrolled to
break and continue are refused as well. There is no loop at run time for either to act on: the
copies are straight-line code inside whatever encloses the for const, so a break would silently
leave that enclosing loop. A loop written inside the body is an ordinary loop and breaks out of
itself as usual.
return works, and Eq and Ord on a tuple are written with it: an unrolled copy is straight-line
code in the enclosing function, so a return in one is an ordinary return.
if const — the arm each instance takes
if const cond picks an arm at compile time, and only that arm is compiled. The condition is a
constant: a value parameter, a const, a call that can be evaluated at compile time, or folded
arithmetic over them. Each instance of a generic and each copy of a for const decides it again, so
one body can hold an arm that makes sense only for some of them:
describe[const N: usize]() -> string
if const N > 64
"big"
elif const N == 0
"empty"
else
"small"
pick[T](x: T) -> int
if const sizeof(T) == 1
x.nothing_here()
else
7
print(describe[100](), describe[0](), describe[3](), pick(5))
big empty small 7
pick(5) compiles: x.nothing_here() is never type-checked for an int, because its arm is not the
one int takes. An ordinary if would check both arms for every instance.
A const member reached through a type parameter is a constant too, so a generic body can ask
the type it was given a question and keep the arm that answers it; the arm not taken is not checked
for that type:
trait Format
keyed() -> bool
struct Json
end Json
struct Compact
end Compact
impl Format for Json
const keyed() -> bool = true
impl Format for Compact
const keyed() -> bool = false
shape[F: Format](x: int) -> string
if const F.keyed()
"keys"
else
x.nothing_here()
print(shape[Json](1))
keys
A chain is decided entirely at compile time. Every elif after an if const is an elif const,
and a plain else covers the rest; a run-time test goes in a plain if inside that else. A
condition not known at compile time is refused:
main()
var n = 3
if const n > 2
print("x")
an 'if const' is decided while compiling, so its condition has to be known then
A generic body is also checked once before any instance, for what does not depend on its parameters.
That check reads no arm of an if const but one: the first arm of if const N == c, a value
parameter against a constant (either way about), is read there with N as c, since only the
instance at c takes it. So [] and Buf[0] are the [N]int and Buf[N] asked for under
N == 0:
struct Buf[const N: usize]
used: usize
zeros[const N: usize]() -> [N]int = if const N == 0 then [] else [0; N]
empty[const N: usize]() -> Buf[N] = if const N == 0 then Buf[0](0) else Buf[N](1)
print(zeros[0]().len, zeros[3]().len, empty[0]().used, empty[4]().used)
0 3 0 1
and what that arm yields is held to it there, called or not:
struct Buf[const N: usize]
used: usize
one[const N: usize]() -> Buf[N] = if const N == 1 then Buf[2](0) else Buf[N](1)
print(1)
the first arm of this 'if const' yields Buf[2], but Buf[N] is expected — the arm is taken only where N is 1, and there they are Buf[1] and Buf[2]
Any other condition, the else, and an elif const are left to the instances, each reading the
arm it takes. A
const function‘s promise is held the same way: the arm an instance
takes is refused there if it cannot run while compiling, and an arm no instance takes may hold
anything.
Every constant position reads the same expressions
What a const function’s body can compute, every place a constant is read accepts: a value
argument, an if const, a const and an array’s length. That includes an enum’s variant and a
comparison of two, T::Make(c, p) in a for const copy, and a field of a struct literal, so a
table of handlers can be built from a walk over an enum’s variants:
enum Op
Add
Sub
apply[const o: Op](x: int) -> int = if const o == Op.Add then x + 1 else x - 1
for const c in Op::Variants
print(c.name, apply[Op::Make(c, ())](10))
const Differ = Op.Add != Op.Sub
var lanes: [if Op.Add == Op.Sub then 2 else 3]int = [1, 2, 3]
print(Differ, lanes.len)
Add 11
Sub 9
true 3
A simple enum’s variant converted to an integer is its discriminant, at the conversion’s type, in
every one of those positions and in a declaration’s own — a field’s length, an @assert beside it.
It converts exactly as it does in a const body, so an explicit discriminant comes out as written:
enum Op: u8
Nop
Load = 4
Store
struct Slots
regs: [u8(Op.Store) - 1]int
@assert(u8(Op.Load) == 4)
opcode[const K: u8]() -> u8 = K
const Next = u8(Op.Store) + 1
if const u8(Op.Load) > u8(Op.Nop)
print(opcode[u8(Op.Store)](), Next, Slots([0, 0, 0, 0]).regs.len)
5 6 4
A condition that does not type is refused for that, not as a test made while the program runs. A
for const variable over a range is a usize, and a function’s parameter takes what any call to it
takes, so a uint parameter wants it converted, fast(uint(k)), as it would anywhere else. (A value
parameter is the one place an integer of any width is read at its declared type, fit-checked by its
value: H[k] over a const N: uint is accepted.)
const fast(x: uint) -> bool = x > 1
for const k in 0..<3
if const fast(k)
print(k)
'x' of 'fast' is uint, but usize was given
What it is for: the catalog covers every tuple
impl[..A: Display] Display for (..A) is one block covering every tuple, and Eq, Ord and Hash
are one block each. Packs are what let the library implement a trait for tuples of every arity: a
pair and a triple are different types, and (..A) is generic over the difference.
A tuple has three shapes an impl may match, and they are found most specific first: the tuple
written out in full, then one arity, then every arity.
trait Tag
tag(self) -> string
impl[..A: Display] Tag for (..A)
tag(self) -> string = "any"
impl[A: Display, B: Display] Tag for (A, B)
tag(self) -> string = "pair"
override impl Tag for (int, int)
tag(self) -> string = "two ints"
print((1, 2).tag(), (1, "x").tag(), (1, 2, 3).tag())
two ints pair any
This is the same “written out before a parameter” ordering an array’s two shapes have, with one more
level. The override is a separate rule: coherence says where a block may be written, and
override says which of two valid blocks answers. A block for a tuple written out in full is the
specific one, so it carries the override.
A member declares one too, and a trait’s member as well
A pack may appear in a method’s own parameter list on the same terms as a value parameter, for the same reason: the list belongs to the member:
struct Row
n: usize
take[..A: Display](*self, t: (..A))
for const i in 0..<A.len
self.n = self.n + t.i.to_string().len
end Row
var r = Row(0)
r.take((1, "abc", true))
print(r.n)
8
A trait’s member takes one too: a member may declare parameters of its own of any kind, and a
pack is one more kind. The cost falls on the trait object, not the declaration: a member with
parameters of its own is not a function until a call names them, so no slot of a &Trait‘s table
can point at it.
The impl writes the same list, matched by position so the names need not agree, and the types are
named at the call, whether that call is on the type or through a bound:
trait Take
take[..A: Display](self, t: (..A)) -> usize
struct Row
n: usize
impl Take for Row
take[..A: Display](self, t: (..A)) -> usize
var total = self.n
for const i in 0..<A.len
total = total + t.i.to_string().len
total
through[S: Take](s: S) -> usize = s.take((1, "abc", true))
print(Row(0).take((1, "abc", true)))
print(through(Row(2)))
8
10
Such a member has no table slot. This is a rule about erasure, not about packs: no slot can hold a function that does not exist until a call names its types. The trait still has an object type, and the object still dispatches every other member; only reaching that member through it is refused (traits).
trait Take
tag(self) -> usize
take[..A: Display](self, t: (..A)) -> usize
struct P
n: usize
impl Take for P
tag(self) -> usize = self.n
take[..A: Display](self, t: (..A)) -> usize = self.n
val o: &Take = P(5)
print(o.tag())
print(o.take((1, "ab")))
'take' of 'Take' declares type parameters of its own, so it is not a function until a call names them
A struct, an enum and a trait may not take a pack, for a different reason: their parameters
are their shape, and there is nothing to spread a list over. A member has no shape of its own, so
that reason does not apply to it.
A written list spreads over a pack
A pack is usually read off the tuple it is handed, but a declaration whose pack appears in no parameter has nothing to read it off, and a written list names it. The pack takes every argument the other parameters leave: those before it take the leading ones and those after it the trailing ones, one type each, and the pack the run between.
struct Pos
x: int
y: int
count[..A]() -> usize = A.len
last[T, ..A](x: T) -> usize = A.len
print(count[int, bool](), count[Pos](), count[(Pos, Pos)](), last[string, int, int]("s"))
2 1 1 2
One type named for a pack is a pack of that one type, never its parts: count[Pos] counts one,
and a tuple named in the list is one type of the pack like any other. A pack bound to one type is a
tuple of one part, (Pos,), wherever (..A) is written.
The same list is written at a member, through a bound, and at an address (&count[int, bool]), and
the argument a pack’s tuple parameter is handed is checked against the types the list named.
A pack stands for one type or more, so the list names every parameter, with at least one type for the pack:
last[T, ..A](x: T) -> usize = A.len
print(last[string]("s"))
'last' takes 2 type arguments or more ('T', 'A'), 'A' being a type pack that stands for one type or more, and 1 was written
A declaration with two packs has no written list, since nothing in one says where the first pack ends; its arguments settle both:
two[..A, ..B](a: (..A), b: (..B)) -> usize = A.len + B.len
print(two[int, int, int]((1, 2), (3,)))
'two' takes two type packs, '..A' and '..B', so a written list cannot say where one ends and the next begins — let the arguments settle them
This is not variadic functions
A pack is a compile-time list of types. C’s ellipsis is a run-time walk over untyped storage
(ffi), and the two are unrelated. There is also no pack expansion: a pack
cannot be spread into a call’s arguments, and (..A, int) cannot append to one. The unrolled loop
takes the place of expansion, in the one direction the catalog needs.
What is deliberately not here
| absent | why, and what to write instead |
|---|---|
type-level arithmetic in a signature (-> [N + 1]T, Buf[N + 1]) | a value parameter may be named in a signature but not computed with there, since a call is solved from it; a body and a field may compute a struct’s value argument, compared by its polynomial |
pack expansion (f(..a), (..A, int)) | a pack may be matched and walked, not spread into an argument list or appended to; use for const instead |
higher-kinded parameters (F[_]) | excluded by design: they push inference toward undecidability, and traits and bounds already serve abstraction over containers |
Explicit type arguments at a call are available, at the heads listed
above. They matter most for a generic function with no
parameters, which has no argument to infer from: buf() and map() are otherwise solved only by
what receives the result, and buf[u8]() states it at the call:
import sysl.buf.buf
var b = buf[u8]()
b.push(3)
print(b.len())
1
Next: modules.