Zane Generics and Type Parameters
This document specifies Zane’s type-parameter system. A type in Zane is a templated function: it takes parameters and is executed to produce a concrete layout. A type definition declares its parameters in a <> header placed after the name (Vector<T Type>), because a type is applied positionally (Vector<Int>) and that ordered header is its architecture. A verb — a function, method, or constructor — has no header: it introduces its type and number parameters inline, at their first marked occurrence in its signature (x T Type, this Buffer<T Type, n @concepts$Int>), and references them by bare name thereafter. Type arguments are written in <> type expressions; they are never written at a constructor or function call.
See also:
syntax.md§2 for the surface syntax of type expressions.types.md§5 fortypeandaliasdeclarations and §3 for constructors.lexical.md§3 for the casing rule that distinguishes types from values.functions.md§5 for the generic-match phase of overload resolution.
Story:
stories/generics.mdexplains the story behind these rules — the parameter model, the<>/()split, the literal-wrapping cost, and the deferred features.
1. Overview
Zane treats a type as a definition that is executed. A type definition takes parameters and produces a concrete layout, the same way a function takes parameters and produces a value. Templating is therefore not a bolt-on feature; it falls directly out of types being executable, and out of types themselves being ordinary compile-time values.
Types are templated functions. A parameterized type lists its parameters in a<>header and produces a result. Applying arguments to those parameters (Vector<Int>) evaluates the template into a concrete type. A library package may take a header the same way, and each argument list names an instance of it (packages.md§2.6).Types use a header; verbs introduce inline. A type’s parameters are applied positionally, so a type keeps its<>header — an ordered, explicit signature. A verb’s parameters are always inferred and never applied positionally, so a verb has no header: it introduces each parameter inline at its first marked occurrence (x T Type,Array<T Type, n @concepts$Int>) and references it bare elsewhere.Inferring fills in a type's value. Every symbol has a value and a type, and a type is itself a symbol — its value is a type, its type is the conceptType. An explicit parameter (T Type) supplies that type-value directly; an inferred parameter (x T Type) leaves it blank, and the compiler reads it off the value argument (§3.1).Parameters are concept-typed. A parameter is declaredname Type(a type parameter) orname @concepts$Int(a number parameter).Typeand@concepts$Intare compiler concept types; the case of a parameter’s identifier follows the parameter’s concept —Tis a type,nis a number.References are bare. Inside a body or a nested type, a parameter is referenced by its bare name (T,n). There is no sigil: a parameter name is introduced once — by a type’s header or by a verb’s first inline occurrence carrying its concept — and the casing rule keeps type parameters and number parameters distinct.A subject may be a parameter. A method with the subjectthis S Typeapplies to a subject of any type (§3.7).A veiled value is reached through verbs and subscripts. A value whose type is a type parameter is a veiled value. A generic body may hold one, call verbs on it and read its subscripts, but never reads its fields or otherwise depends on its mould (§3.8).- **
<>describes architecture,()constructs values.** A<>type expression is a compile-time description that lives in the type system. A()` call is a runtime construction that lives in the value system. They are different mechanisms, not two syntaxes for one idea. No type arguments at calls. A constructor or function is always called by its bare name with(). Type and number parameters reach it either inferred from the value arguments (inline-introduced parameters) or passed as ordinary arguments (Type/@concepts$Intvalue parameters).Four container primitives.@primitives$Array<T, n>isncontiguous elements of typeTand a value type;@primitives$ArrayRef<T, n>has the same layout and is a reference type.@primitives$List<T>and@primitives$ListRef<T>are their dynamically sized counterparts, a value type and a reference type.coredeclaresArray,ArrayRef,List, andListRefover them. Each has a subscript counted from 1, a list grows and shrinks bypush,remove, andswapRemove, and aListcopies, compares, hashes, and sorts ranges of its elements (§8.6).
2. Types as Templated Functions
2.1 A type definition takes parameters
A parameterized type declares its parameters in a <> header, then produces a layout. The header is the type’s signature; the right-hand side is what it produces.
type Vector<T Type> = struct {
x T;
y T;
}
Vector is a template with one type parameter, T. It is not yet a concrete type. Applying an argument evaluates it:
Vector<Int> // evaluates the template with T = Int
Vector<Float> // evaluates the template with T = Float
Each distinct application is a distinct concrete type with a known, fixed layout.
2.2 Parameters may be types or numbers
A header may mix type parameters and number parameters. A type parameter is declared with the Type concept; a number parameter with @concepts$Int, the concept type an integer literal carries (lexical.md §7).
type Buffer<T Type, n @concepts$Int> = struct {
data Array<T, n>;
}
Buffer<Int, 64> // T = Int, n = 64
T ranges over types; n ranges over compile-time integers. The argument 64 is an integer literal, so it fills an @concepts$Int slot the way any argument fills a slot of its own type. Inside the body, n may also be read as a number value — for example, to compute a length — exactly as data Array<T, n> uses it to fix the storage size.
2.3 Concrete (non-parameterized) types
A type with no parameters needs no <> header. Its <>-free name is already a complete type.
type Wrapper = struct {
vec Vector<Int>;
arr Array<Int, 10000>;
}
Wrapper is concrete: every field type is fully applied, so Wrapper’s layout is known. A named type is always declared with type Name = struct { ... } or type Name = #struct { ... } (see types.md §2); the struct { ... } and #struct { ... } bodies are type expressions and do not name a type on their own.
2.4 A type may not hold itself at a growing argument
A type definition may lead back to itself through its members, directly or through other types. Each time it does, every argument it passes back MUST either be one of its own parameters as written or name none of them. This is the growing-argument rule. A definition that passes a parameter back inside a larger argument is a compile-time error:
type Grow<T Type> = variant { leaf T; more Grow<Array<T, 2>>; } // ILLEGAL
Grow<Int> holds a Grow<Array<Int, 2>>, which holds a Grow<Array<Array<Int, 2>, 2>>, with no last type in the chain. Each value is finite, since it ends at a leaf. The types are not: every application is a distinct concrete type with a layout of its own (§2.1), and evaluating Grow<Int> never reaches a set of types it has already produced. Boxing cannot help, because boxing makes each type on a cycle finite (adt.md §4) and here there is no end to the types.
Leading back at the same arguments, at shuffled ones, or at fixed ones produces finitely many types, so each is legal:
type Chain<T Type> = variant { last T; more Chain<T>; }
type Swap<A Type, B Type> = variant { first A; second B; more Swap<B, A>; }
type Mixed<T Type> = variant { last T; more Mixed<Int>; }
The rule follows every member: inline and boxed members, & fields, a distinct type’s definition, and the element types of Array, ArrayRef, List, and ListRef. It also follows a cycle through other generic types, where the argument may grow on any step:
type A<T Type> = variant { last T; next B<Array<T, 2>>; } // ILLEGAL
type B<U Type> = variant { last U; next A<U>; }
The error is reported at the declaration, on the member that passes the larger argument, whether or not anything applies the type. Unlike a wrong-kind argument (§3.6), such a type is wrong for every argument, so its declaration is where the mistake is.
Story:
stories/generics.md— “A type may not hold itself at a growing argument”.
3. The Parameter Model
3.1 Symbols have a value and a type
Every symbol — a variable or a parameter — has a value and a type. x Int declares a symbol x of type Int; its value at run time might be Int(3).
A type is itself a symbol of exactly this form: a name with a value and a type. The symbol T has a value — a concrete type such as Int — and a type: the concept Type. So T Type declares T the same way x Int declares x: a symbol named T, of type Type, whose value is a type.
That is the whole parameter model. The two parameter forms differ only in which part you supply:
Unit func (x Int) // x : value Int(3), type Int
Unit func2(x T Type) // x : value Int(3), type T — and T : value Int, type Type
Unit func3( T Type) // T : value Int, type Type
func3(T Type)declares the type-symbolTdirectly. The caller supplies its value:func3(Int)givesTthe valueInt. This is explicit passing.func2(x T Type)declares a value-symbolxwhose type isT, but does not giveTa value. The caller supplies a value forx—func2(Int(3))— and the compiler readsT’s value off it:Int(3)has typeInt, soTisInt. This is inference.
Inference is therefore not a separate mechanism. It is the same T Type binding with its value left blank, to be filled from the value argument. Whether the parameter has a leading value identifier determines which form it is: write it (x T Type) and T’s value is recovered from that value; omit it (T Type) and the caller gives T’s value directly.
§3.2 states where a parameter is introduced, and §5.2 and §5.3 state how each form reaches a call; each section applies this same idea.
A number parameter is the one asymmetry. A type can be the type of a value, so a type parameter can be recovered from a value (x T Type). A number cannot be the type of a value — x n @concepts$Int is meaningless — so a number parameter has no value to read it from, and is instead inferred structurally, from a nested type that carries it (Array<T Type, n @concepts$Int>, where n comes from the argument’s length).
Story:
stories/generics.md— “The parameter model” develops this, including why dropping the leading name is a legible edit rather than an arbitrary mode flip.
3.2 Where parameters are introduced
A type and a verb introduce their parameters in different places, for one concrete reason: a type is applied positionally, a verb is not. A parameterized package declares its parameters in a header as a type does, because an import applies them positionally too (packages.md §2.6); inside the package a verb references them bare, as it references any parameter already introduced.
A type definition declares its parameters in a <> header placed immediately after the name. The header is the type’s signature — it fixes the parameters and their order, which is exactly what a use site fills positionally (Vector<Int>, Buffer<Int, 64>).
type Vector<T Type> = struct { ... } // header: one type parameter
type Buffer<T Type, n @concepts$Int> = struct { ... } // header: a type then a number
A verb — a function, method, or constructor — has no header. It introduces each type or number parameter inline, at the parameter’s first marked occurrence — the first place the name carries its concept (Type / @concepts$Int):
Vector<T>(x T Type, y T Type) { ... } // T introduced on a value parameter
T head(arr Array<T Type, n @concepts$Int>) { ... } // T, n introduced inside a nested type
Int size(this Buffer<T Type, n @concepts$Int>) { ... } // T, n introduced on the subject
A verb has no header because it never needs one: its parameters are always inferred (§5) and never applied positionally, so there is no order to fix and nothing for a header to declare.
A parameter name is introduced by its first marked occurrence and referenced bare everywhere else in the signature. A bare reference may appear before the introduction: in T head(arr Array<T Type, n @concepts$Int>) the return type T is a bare reference even though it is written first, because the introduction is the marked T Type in the parameter list. Within one verb signature every occurrence of a parameter name is the same parameter, so T add(x T Type, y T Type) constrains x and y to a single type T; a non-introducing occurrence may be written bare (T) or may repeat the concept (T Type), and both denote the same parameter.
An introduction on a value parameter’s own type carries that parameter’s passing mode (memory.md §2.9) the way a concrete type does: x T Type borrows, x ^T Type takes an owner, and x &T Type takes a reference. The marker belongs to the parameter, so a bare T elsewhere in the signature names the type without it. A verb that stores its argument needs the marker, since an introduction is the only place T is written on that parameter:
Box<T>(value ^T Type) => init{value;} // takes the owner, to store it
Pin<T>(at &T Type) => init{at;} // takes a reference, to store it
A nested introduction carries a marker in one place: the element of a value parameter’s own @concepts$Array, the concept type an array literal carries (syntax.md §2.9). Each element of the literal is passed on its own, so the marker there is the mode each element is passed in, as the marker on a parameter’s own type is that parameter’s: values @concepts$Array<^T Type, n @concepts$Int> takes each element’s owner, and values @concepts$Array<&T Type, n @concepts$Int> takes a reference to each (§8.4). Every other nested introduction in a type argument is bare, since no other type argument is passed.
When a value parameter’s own type is a function type (syntax.md §2.13), its return slot and parameter slots take an introduction too, in the form a value parameter’s own type uses: T Type, ^T Type or &T Type. A slot of a function type already carries a passing mode, so the marker on an introduction there is that slot’s mode. As every introduction does, the parameter belongs to the verb whose signature contains the function type. The verb is therefore generic, and each function value a call passes to it is not: the compiler reads the parameter off the function value’s own type. Here make ^T Type[Int] introduces T at the return slot of make’s type, and a call passes a lambda that returns an owner of T:
ArrayRef<T, n>.fill(n @concepts$Int, make ^T Type[Int]) => init{
_value = @primitives$ArrayRef.fill(n, make);
}
jars ArrayRef<Jar, 3> = ArrayRef.fill(3, ^Jar(i Int) => Jar(i)); // T is Jar
These rules let a reader and the compiler read a bare T unambiguously. In a type, a name in the enclosing header is a parameter. In a verb, a name introduced (marked with its concept) anywhere in the signature is a parameter for that whole signature. A name that is never introduced is a concrete type.
Story:
stories/generics.md— “A type parameter’s introduction carries the passing mode:^T Typeand&T Type”. Story:stories/memory.md— “AnArrayRefconstructor names the mode of its elements”. Story:stories/generics.md— “A function type’s slot may introduce a type parameter”.
3.3 Type and @concepts$Int are concept types
Type and @concepts$Int are compiler-provided concept types. Type is the concept of a type; @concepts$Int is the concept of a compile-time integer, and is the same concept type an integer literal carries (syntax.md §2.8). Like every concept type, they may appear only in parameter positions and MUST NOT be used as storage (see syntax.md §2.8 and §2.11). A value of concept type Type is a type; a value of concept type @concepts$Int is a compile-time integer, whether it arrives as a number argument in a type expression or as an integer literal passed to a verb, because both are leaf concept types (syntax.md §2.8). Both are available at compile time and may be used in the positions their concept type allows — a Type value in a type position, an @concepts$Int value in a number position.
These are the only two concept types a type’s <> header holds, and a parameterized package’s header holds the same two (packages.md §2.6). A header parameter’s value is substituted into the body it parameterizes, so every such parameter ends in a slot of a compiler-provided type — @primitives$Array<T, n> takes a type and an integer (§8.1) — and its concept is the concept of the argument that slot accepts: a type, or an integer. A header entry of any other concept type is a compile-time error. A new kind of header parameter comes into being only with a compiler-provided type whose slot accepts it. A verb may still declare a value parameter of another concept type, such as @concepts$Float (§5.4) or @concepts$Block, to accept a literal or a block argument.
Story:
stories/generics.md— “Splitting integer and float literals”.
3.4 References are bare; casing separates types from numbers
A parameter is referenced by its bare name. The casing rule (see lexical.md §3) separates the two concepts: an uppercase name is a type, a lowercase name is a number. Within one declaration, two occurrences of T are the same parameter, and two different names (T and U) are independent.
type Pair<T Type, U Type> = struct {
first T;
second U;
}
first and second are independent; a use site may apply different types to each (Pair<Int, Float>).
3.5 Number parameters resolve to values in body positions
A number parameter referenced in a body position (not a type position) resolves to its number value. This is how a method on a parameterized type can read a size as an ordinary number.
Int size(this Buffer<T Type, n @concepts$Int>) {
return n;
}
Here n in the return position is the number the use site supplied for that parameter. The subject type Buffer<T Type, n @concepts$Int> introduces T and n inline; the return position references n. The Array<T, n> layout inside Buffer uses the same n to fix the storage size.
See also:
effects.md§2 — a number parameter read in a body position is a read-only value-like binding.
3.6 A wrong-kind type argument is reported where the concrete type enters
A type parameter ranges over value and reference types alike. Whether a given type fits is decided by where the parameter ends up: a value mould may hold only value types, so a reference type that reaches a value-type field makes the instantiation ill-formed (memory.md §2.10). A reference mould holds either kind. An &T takes only a reference type (memory.md §2.4), so a value type that fills T there, on a field or on a verb’s parameter, is ill-formed the same way. A ^T takes an owner or a borrow, never a reference (memory.md §2.9), so an & type that fills T there is ill-formed too, and so is ListRef<&U> (§8.5). A List is a value type, so it takes only a value type (§8.3).
type Box<T Type> = #struct {
value T;
}
Box<T>(value ^T Type) => init{value;}
a Box(MainCharacter()); // legal: a reference type may hold a reference type
b Box(Int(3)); // legal: and a value type
Such an error is reported at the origin of the type: the first position, following the type down from where it is written into the parameters it fills, at which it is a concrete type rather than a parameter. An explicit type argument is its own origin. An inferred one originates at the value argument it is read from. A parameter that only forwards a type to another parameter is never the origin, so a generic verb that passes T on is not where its caller’s mistake is reported. The diagnostic MUST identify the path from the origin to the slot that rejects the type, since the reported line alone does not show why the type was wrong.
type Pair<T Type> = struct {
first T;
}
Pair<T>(first T Type) => init{first;}
Pair<T> wrap(x T Type) => Pair(x);
alias Crew = Pair<MainCharacter>; // error at MainCharacter: the explicit argument is the origin
q Pair(MainCharacter()); // error at MainCharacter(): the inferred T originates there
r wrap(MainCharacter()); // error at MainCharacter(), not inside wrap
Story:
stories/generics.md— “A wrong-kind type argument is reported at its origin”.
3.7 A method’s subject may be a type parameter
A method may introduce a type parameter as its subject’s whole type. this S Type makes the method apply to a subject of every type, with S the subject’s type at each call:
Unit work(this S Type, step Bool[this S] mut) mut {
this!@controlflow$work(step);
return Unit();
}
machine!work(Bool(this Machine) mut { ... }); // S is Machine
The introduction carries no passing marker, because the subject takes none (syntax.md §3.2): this S Type is a borrow of the caller’s value or owner, mutable when the method is mut (functions.md §2.4). S may be a value type or a reference type, and §3.6 applies to it as to any inferred parameter, with the subject expression as its origin.
The subject is a veiled value (§3.8), so the body reaches it only through verbs and subscripts: a method on it, an operator, a subscript of it, a function value whose type names S, an intrinsic generic over its subject (control-flow.md §4.1), or another verb generic over S. Such a method has no declaring subject type, so method lookup reaches it through the spelling the file has for its package (functions.md §6.1).
Story:
stories/generics.md— “A method’s subject may be a type parameter”.
3.8 A veiled value is reached only through verbs and subscripts
A veiled value is a value whose type is a type parameter, a parameterized package’s own included (packages.md §2.6): x in Float read(x T Type), a value of type App inside package clap<App Type, Dsp Type, Info Type, Params Type>, the subject of this S Type (§3.7), a local of type T, or the result of a call or field read that yields a T. A generic body may use a veiled value in two ways:
- Hold it. The body may bind it, store it in a slot of type
T, return it, and pass it on. - Reach it through a verb or a subscript. The body may call a method with it as the subject (
x:level(),x!reset()), pass it to a function (clamp(x)), apply an operator to it (x + y,x == y), read a subscript of it (x[i]), and construct a value of its type (T(...),T.name(...)).
Every other use of a veiled value is a compile-time error, reported where the body writes it, whether or not any call instantiates the body:
- reading or writing a field (
x.value); - reading an enum map property (
adt.md§6),x.colorName; - naming an enum member through the parameter,
T.red; - a
matchon the value (adt.md§5); - calling the value as a function value,
x(1).
A call written T.name(...) is always a named-constructor call, resolved per instance like every other verb below. A named constructor’s name differs from every case of its type (types.md §3.4), so T.done(Unit()) never names a case: when the type that fills T declares no constructor T.done, the instantiation is ill-formed, and the compiler reports the error at the type’s origin.
type Knob = struct { value Float; }
Float level(this Knob) => this.value;
Float read(x T Type) => x:level(); // legal: a method call
Float peek(x T Type) => x.value; // error: a field of a veiled value
T twice(x T Type) => x + x; // legal: an operator
The error on peek holds even when every caller passes a Knob: the rule judges a body by the operations it applies to T, never by the types that fill it. A body that needs a field’s value calls a method that the type’s package declares, such as level above. Field visibility stays the type package’s decision (types.md §2.3), and every mould can meet the body’s demands, because every mould can declare verbs: methods and functions (functions.md §6.3), operators (operators.md §2.2), and constructors (types.md §3.1).
The rule covers only values whose whole type is a type parameter. A generic body sees its type’s own structure, so it keeps full access to that structure:
- In a method of
Box<T>,this.valuereads a field ofBox. The field’s type isT, so the result is a veiled value:this.value:weight()is legal andthis.value.weightis an error. - A value of type
Pair<T>orList<T>is not veiled;p.firstandlist[i]are legal, and only what they yield falls under the rule. - A parameter of function type, such as
makeinArrayRef.fill(n @concepts$Int, make ^T Type[Int])(§3.2), is not veiled, somake(i)is an ordinary call of a function value. - A number parameter resolves to a number value in body positions (§3.5) and is not veiled.
Each kind of verb on a veiled value resolves per instance, against the concrete type that fills the parameter, and each searches its own place:
| Verb | Where the compiler looks | Can the type argument’s package supply it? |
|---|---|---|
Method, x:level() |
the home package of the subject’s type first (functions.md §6.1) |
yes |
Operator, x + y |
the operands’ home packages (operators.md §2.2) |
yes |
Constructor, T(...), T.name(...) |
the constructors of the type that fills T (types.md §3.1) |
yes |
Function, clamp(x), util$clamp(x) |
the generic body’s own package and the spellings its file has (packages.md §3.6) |
no |
A function is therefore never found through the type argument: one declared in the type argument’s own package is out of the body’s reach unless the body’s file imports that package itself, and a body that needs the type’s own behaviour calls a method: x:f() already searches the home package of x’s type first. When the concrete type has no verb that matches a call, the instantiation is ill-formed, and the compiler reports the error at the type’s origin, with the path from there to the call, as §3.6 requires for a wrong-kind type argument.
The rule fixes the operations a body may apply to its type arguments and leaves open how a compiler checks and compiles it. Under it, a future constraint on a type parameter (§9) is a list of verb signatures, and a generic function value’s representation (§9) needs one entry per verb and never a type argument’s field layout.
Story:
stories/generics.md— “Only verbs reach a veiled value”.
4. Type Expressions (<>)
4.1 <> is the application syntax of the type system
A type expression applies arguments to a parameterized type. It is a compile-time, structural description: it describes the architecture of a value’s type, and it does not build a value. Type expressions appear wherever a type is expected:
type Wrapper = struct {
vec Vector<Int>;
arr Array<Int, 10000>;
}
Vec2 transform(this Vec2, mat Matrix<Float, 3>) { ... }
Struct fields, function and method signatures, return types, aliases, and nested arguments are all type-expression positions.
4.2 Arguments are positional
Arguments fill the header’s parameters left to right. A type argument fills a Type slot; a number argument fills an @concepts$Int slot.
Array<Int, 10000> // T = Int, n = 10000
Matrix<Float, 3> // type then number
A type argument may itself reference a parameter that the surrounding scope has in scope. Inside a declaration whose header binds T and n, the expression Array<T, n> forwards both:
type Buffer<T Type, n @concepts$Int> = struct {
data Array<T, n>;
}
4.3 Casing makes <> unambiguous
The token before < in a type expression is always a type, which is always uppercase-initial (see lexical.md §3). A comparison operator never has a type on its left. This is why Vector<Int> parses as a type application while a < b parses as a comparison: the case of the left operand’s first letter determines which. Case-sensitive parsing makes the <> type syntax viable.
4.4 Inside a verb, a <> entry may introduce a parameter
A type expression normally applies arguments. Inside a verb signature — where there is no header — the same <> brackets are also where a nested type or number parameter is introduced: an entry that carries its concept (T Type, n @concepts$Int) introduces a parameter; a bare or concrete entry references or applies as usual.
Int size(this Buffer<T Type, n @concepts$Int>) => n; // legal: T and n introduced here
T first(arr Array<T, n>) { ... } // ILLEGAL in a verb: nothing introduces T or n,
// so the bare names are undefined
So a <> list in a verb may mix three kinds of entry: an introduction (T Type), a reference to a parameter already introduced in the same signature (T) or in the header of a parameterized package that declares the verb (App), and a concrete argument (Int). An introduction there is bare, except as the element of a parameter’s own @concepts$Array, where it may carry the mode each element is passed in (§3.2). A type definition’s or a package’s header holds only introductions; a type expression outside any verb — a field, a return type of a non-generic verb, an alias — holds only references and concrete arguments.
5. Constructor and Function Calls (())
5.1 () calls; a call never takes a <> list
A constructor call builds a value at run time; a function call runs a function. Both are invoked by bare name, and MUST NOT carry a <> list — type arguments belong to type expressions only.
vec Vector(Int(2), Int(3)); // legal: bare-name constructor call
vec Vector<Int>(Int(2)); // ILLEGAL: a call takes no <> list
A type or number parameter reaches a callable in one of two ways: inferred (introduced inline on a parameter’s type or in a nested type) or passed explicitly (declared as a Type/@concepts$Int value parameter).
5.2 Inferred parameters (inline)
A type or number parameter introduced inline in a verb’s signature is inferred from the value arguments. The caller writes no <>; the compiler deduces each parameter from the argument types.
Vector<T>(x T Type, y T Type) { // T introduced inline; x and y share it
return init{ x; y; }
}
vec Vector(Int(2), Int(3)); // T inferred as Int from the arguments
A constructor’s name is its return type, so a constructor for a parameterized type names the applied type (Vector<T>), where the <...> holds bare references to the inline-introduced parameters — it carries T, not T Type, so it is a type expression, not a reintroduced header. The call is still by bare name (Vector(Int(2), Int(3))); only the declaration shows the applied return type.
5.3 Explicit parameters (Type / @concepts$Int value parameters)
A type or number can instead be passed as an ordinary argument by declaring a value parameter of concept type Type or @concepts$Int. The argument is then written positionally in (), like any other value.
The distinction from §5.2 is purely structural, read off the parameter’s shape. An inferred parameter rides on a value parameter’s type (x T Type — a value x whose type is the fresh parameter T) or in a nested type, so a value argument fixes it. An explicit parameter is the value parameter (T Type — a value parameter named T of concept Type), so the caller passes it directly. The first carries a value name before the concept-typed type; the second does not.
Vector<T>(T Type) {
return init{ x = T(0); y = T(0); }
}
Array<T, n>(T Type, n @concepts$Int) {
// zero-initialise n elements of type T
}
vec Vector(Int); // Int passed as the type argument
arr Array(Int, 10000); // Int passed as the type, 10000 as the size
A Type value parameter is usable as a type inside the body (for example, T(0)); an @concepts$Int value parameter is usable as a number. This is the practical payoff of types being compile-time values: a type handed to a constructor is just an argument the body can execute.
Story:
stories/generics.md— “No turbofish: passing types as values” explains why passing a type directly exists: it makes the no-turbofish decision affordable rather than crippling.
5.4 Concept-typed literals must be wrapped
A bare source literal carries a compiler concept type, not a concrete storage type. Whether it may drive inference turns on whether that concept type fixes a concrete type. An @concepts$Int, @concepts$Float, or @concepts$String does not — an integer literal may become an Int or any other type whose package declares a conversion from @concepts$Int, and the compiler does not choose — so such a literal MUST NOT drive inference of a type parameter. Wrap it in its destination type:
vec Vector(Int(2), Int(3)); // legal: each argument is a concrete Int
vec Vector(2, 3); // ILLEGAL: literals cannot drive inference of T
A container literal is the case where the concept type does carry concrete parameters. A [ ] literal’s @concepts$Array<T, n> (see syntax.md §2.9) fixes an element type and a length, and §6.1 reads both from it; a { } map literal’s @concepts$Map<K, V> (syntax.md §2.10) fixes a key type and a value type on the same terms. The wrap is therefore required of the entries — a map literal’s keys and values alike — rather than of the bracket. The map literal below stands on its own, since the container types that consume one are not specified in this version (§9):
arr Array([Int(1), Int(2), Int(3)]); // legal: elements are concrete, so T = Int and n = 3
arr Array([1, 2, 3]); // ILLEGAL: bare elements fix no T, as in Vector(2, 3)
{String("k"), Int(1);} // entries are concrete, so K = String and V = Int
{"k", 1;} // bare entries fix neither K nor V, so this drives no inference
This single explicit wrap at the call site is the deliberate cost that replaces a <> type-argument list at every call.
A verb may take that cost on itself instead, by declaring a parameter of the literal’s own concept type and doing the wrapping in its body. @concepts$Int and @concepts$Float are distinct types, so a verb may overload on them (functions.md §4.3), and a call with bare literals selects its overload in the direct phase of resolution (functions.md §5):
Vector<Int> vec(x @concepts$Int, y @concepts$Int) => Vector(Int(x), Int(y));
Vector<Float> vec(x @concepts$Float, y @concepts$Float) => Vector(Float(x), Float(y));
a Vector<Int> = vec(2, 3); // legal: selects the @concepts$Int overload
b Vector<Float> = vec(2.5, 3.5); // legal: selects the @concepts$Float overload
c Vector<Float> = vec(2, 3.5); // ILLEGAL: no overload takes an integer and then a float
No implicit constructor has a concept type as its destination (types.md §4.4), so an integer literal never selects a @concepts$Float overload and a float literal never selects a @concepts$Int one.
Story:
stories/generics.md— “Splitting integer and float literals”.
6. Array Construction
Array<T, n> illustrates both mechanisms together. The element type and the size are both parameters; a constructor supplies them by inference or by explicit argument.
6.1 Inferred from a literal
Array<T, n>(values @concepts$Array<T Type, n @concepts$Int>) {
// T and n inferred from the literal
}
arr Array([Int(1), Int(2), Int(3)]); // T = Int and n = 3 inferred from the literal
The value-parameter type @concepts$Array<T Type, n @concepts$Int> introduces T and n inline and lets the compiler read both from the literal’s element type and length. The parameter names the concept type an array literal actually carries (syntax.md §2.9), which is what lets the constructor accept the literal directly and lower it. Declaring the parameter as Array<T, n> instead would demand a conversion into the very type being constructed; no implicit constructor is involved here, and §4.3’s no-chaining rule is never engaged.
6.2 Explicit type and size
Array<T, n>(T Type, n @concepts$Int) { // called as Array(Int, 10000)
// zero-initialise n elements of type T
}
arr Array(Int, 10000); // T = Int passed, n = 10000 passed
Used when no literal is available — for example, a zero-initialised array of known length.
6.3 Type expression
type Matrix = struct {
data Array<Float, 9>; // architecture: 9 contiguous Floats, stored inline
}
7. Why Size Must Be in the Type
7.1 The cost is loss of uniform stride
It is tempting to leave array size out of the type, since the stack pointer is just a register and an array never resizes after construction. C99 VLAs do exactly this for locals. The real cost of a runtime-sized type is not stack allocation — it is the loss of uniform stride. If two values of one type can have different sizes, the following all break:
arr[i]— indexing needsbase + i * stride, andstridemust be a compile-time constant for O(1) access.- Embedding in a struct — the outer struct’s layout becomes unknown.
- Copying — requires a runtime size query.
- Calling conventions — ABIs assume fixed-size parameters.
7.2 The break propagates outward
An array of variable-size structs loses uniform stride; a struct containing a variable-size struct loses uniform stride; and so on up the containment chain.
7.3 Conclusion
Baking the size into the type (Array<T, n>) is the mechanism that guarantees every value of a given type is the same number of bytes. That guarantee makes indexing, copying, embedding, and calling all cheap.
Story:
stories/generics.md— “Size is part of the type” weighs the VLA alternative and the stride-loss propagation in full.
8. Container Storage Primitives
8.1 Compiler-provided layout
@primitives$Array<T, n> is a value-type storage primitive: n contiguous elements of type T. Its byte size is n * sizeof(T). It has no header. core declares Array<T, n> over it, as it declares each of the fundamental types over a storage primitive (types.md §2.6), and source writes that name. Both parameters may be supplied as concrete arguments (Array<Int, 10000>), forwarded from an enclosing scope (Array<T, n>), or inferred by a constructor from a literal (Array([Int(1), Int(2), Int(3)])).
8.2 Array is the fixed-size storage base case
Other fixed-size containers (vectors, matrices) are defined in terms of Array and need no extra compiler support.
8.3 List is the dynamically sized value primitive
@primitives$List<T> is the dynamically sized counterpart of @primitives$Array<T, n>: a sequence of T whose length changes at runtime, so its type carries no n. It is a value type, so its T is a value type (memory.md §2.10), and a value type may contain one. Its elements live in the dynamic region behind a fixed-size handle (memory.md §3.6), so a type that holds one stays statically sized. A copy of a list copies every element into a block of its own, as a copy of a String copies its bytes (memory.md §2.3). core declares List<T> over it as a value type.
names List(String);
names!push(String("Ada"));
copy List<String> = names;
copy[Int(1)] = String("Alan"); // names[Int(1)] is still "Ada"
Story:
stories/generics.md— “A container whose size the type cannot carry”. Story:stories/generics.md— “Listbecomes a value, andListReftakes the reference form”.
8.4 ArrayRef is the fixed-size reference primitive
@primitives$ArrayRef<T, n> is a reference-type storage primitive: n contiguous elements of type T, laid out as @primitives$Array<T, n> is. T may be a value type, a reference type, or an & type, since a reference type may contain any of them (memory.md §2.10). core declares ArrayRef<T, n> over it as a # reference type. Its size is statically known, so it lives inline in the fixed-size region like any other statically sized owner (memory.md §3.5).
An ArrayRef is built from an array literal by one of three constructors, or by ArrayRef.fill, which calls a lambda once per position, in order, with that position’s 1-based index. The lambda takes the position as a type I, which the compiler reads off the lambda: @primitives$I64, or a type with an implicit constructor from @primitives$I64 (types.md §4.1). Each position reaches the lambda through that constructor, as a worker’s step result reaches @primitives$Bool (control-flow.md §4.1), so the lambda below takes core’s Int:
counts ArrayRef([Int(1), Int(2), Int(3)]);
squad ArrayRef.roaming([Enemy(Int(1)), Enemy(Int(2)), Enemy(Int(3))]);
targets ArrayRef.refs([ally, rival]);
grid ArrayRef.fill(100, ^Enemy(i Int) => Enemy(i));
The three literal constructors differ only in the passing mode their parameter writes on the element type, and each element of the literal is passed in that mode, as an argument is passed to a parameter declared with it (memory.md §2.9), then stored into the matching element of the result:
ArrayReftakes@concepts$Array<T Type, n @concepts$Int>, so each element is borrowed. Storing through a borrow copies a value, so a value is copied in, and so is an existing&value, which makesTan&type. An owner of a reference type cannot be stored through a borrow, so it is illegal here.ArrayRef.roamingtakes@concepts$Array<^T Type, n @concepts$Int>, so each element is taken. An owner moves in, so it is a roaming owner or a temporary (lifetimes.md§1.2), and a value is copied.Tis never an&type (§3.6).ArrayRef.refstakes@concepts$Array<&T Type, n @concepts$Int>and builds anArrayRef<&T, n>, so each element is a settled place that mints a reference, or an existing&Tvalue that is copied (memory.md§2.8).Tis a reference type (§3.6).
These are ordinary parameter types (§3.2), so a constructor or function declared in Zane may write any of the three and passes a literal’s elements the same way.
Above, ally and rival are settled owners and targets is an ArrayRef<&Enemy, 2>. Since ArrayRef.refs takes each element on its own, one literal may hold both kinds: with scout an &Enemy, ArrayRef.refs([ally, scout]) mints a reference from ally and copies scout. Such a literal has no element type of its own (syntax.md §2.9), so it is passed only as &T elements.
Each element of the literal comes to rest in the matching element of the result, under every literal constructor. An ArrayRef with & elements therefore carries a reference to each owner its literal named (lifetimes.md §1.10), and every store of it compares those owners’ scopes with the destination’s, as for a resting place a verb’s signature records (lifetimes.md §1.11).
Its elements are fixed storage: they are all present from construction and never come or go, so each element takes its root’s state, settled or roaming, as a struct field does (memory.md §2.8.1). An element of a settled ArrayRef of a reference type may be referenced. An element is overwritten in place and is never moved out, under either kind of root.
Story:
stories/memory.md— “ArrayRef: a fixed reference container whose elements can be guested”. Story:stories/memory.md— “Passing stays implicit, and anArrayReftakes its element type”. Story:stories/memory.md— “AnArrayRefconstructor names the mode of its elements”. Story:stories/generics.md— “A function type’s slot may introduce a type parameter”.
8.5 ListRef is the dynamically sized reference primitive
@primitives$ListRef<T> is a reference-type storage primitive with List’s layout (§8.3). T may be a value type or a reference type. core declares ListRef<T> over it as a # reference type.
A ListRef grows by taking each new element through a ^T parameter, so an element of a reference type is an owner that moves in, and its T is never an & type (memory.md §2.9); a fixed set of references is held in an ArrayRef (§8.4). Its elements are dynamic storage: they come and go while the list lives, so each is roaming whatever the list’s state, and none may be referenced (memory.md §2.8.1).
squad ListRef(Enemy);
squad!push(Enemy(Int(1)));
squad[Int(1)].hp = Int(5);
Story:
stories/memory.md— “No growable list of references”. Story:stories/generics.md— “Listbecomes a value, andListReftakes the reference form”.
8.6 Operations on the container primitives
Each container primitive has a subscript that names the element at a position, counted from 1 (control-flow.md §5.1):
(this @primitives$Array<T Type, n @concepts$Int>)[index @primitives$I64]
(this @primitives$ArrayRef<T Type, n @concepts$Int>)[index @primitives$I64]
(this @primitives$List<T Type>)[index @primitives$I64]
(this @primitives$ListRef<T Type>)[index @primitives$I64]
A position outside the container stops the program (control-flow.md §5.2). The element is a place, whose state §8.4 and §8.5 give (functions.md §2.9).
@primitives$List(T Type) and @primitives$ListRef(T Type) each build an empty list whose elements are of type T, as in @primitives$List(@primitives$I64). A list grows and shrinks through four methods. Each has one overload whose subject is a List and one whose subject is a ListRef, and this section writes only the List forms:
@primitives$Unit push(this @primitives$List<T Type>, value ^T) mut
@primitives$I64 size(this @primitives$List<T Type>)
^T remove(this @primitives$List<T Type>, index @primitives$I64) mut
^T swapRemove(this @primitives$List<T Type>, index @primitives$I64) mut
pushaddsvalueafter the last element. It takesvaluethrough a^Tparameter, so an owner of a reference type moves in, andpushcopies a value in (memory.md§2.9).sizegives the number of elements.removemoves the element atindexout of the list and returns it. The later elements each move down one position and keep their order.swapRemovemoves the element atindexout and returns it asremovedoes, then moves the last element into the freed position. Only one element moves, soswapRemovedoes not keep the order of the elements.
A position outside the list stops remove and swapRemove (control-flow.md §5.2). An element that moves out of its place is no longer in the list (lifetimes.md §2.1).
A List, whose elements are values, also has methods that work on a range: count elements from the position start. Each range lies in its list, or the program stops (control-flow.md §5.2). A ListRef has none of them.
@primitives$Unit appendRange(this @primitives$List<T Type>, from @primitives$List<T>, start @primitives$I64, count @primitives$I64) mut
@primitives$Unit appendOwn(this @primitives$List<T Type>, start @primitives$I64, count @primitives$I64) mut
@primitives$Unit copyRange(this @primitives$List<T Type>, at @primitives$I64, from @primitives$List<T>, start @primitives$I64, count @primitives$I64) mut
@primitives$Bool sameRange(this @primitives$List<T Type>, start @primitives$I64, other @primitives$List<T>, otherStart @primitives$I64, count @primitives$I64)
@primitives$I64 hashRange(this @primitives$List<T Type>, start @primitives$I64, count @primitives$I64)
appendRangeadds copies of the range offromafter the list’s last element, in order.appendOwndoes the same with a range of the list itself. Amutcall cannot also borrow its own subject (memory.md§2.9), soappendRangecannot name the list asfrom.appendOwncopies the range as it was before the call.copyRangeoverwrites thecountelements from the positionatwith copies of the range offrom. Both ranges lie in their lists, and the list’s size does not change.sameRangegives whether the range of the list and thecountelements ofotherfromotherStartare equal, element by element.hashRangegives a hash of the range’s elements. Two ranges whose elements are equal have the same hash within one run of a program, and the value is not otherwise specified.
Two methods reorder a List:
@primitives$Unit sort(this @primitives$List<T Type>) mut
@primitives$Unit sortBy(this @primitives$List<T Type>, keys @primitives$List<K Type>) mut
sortsorts the elements in ascending order.sortByreorders a list whose elements are positions inkeys, so that the keys they name ascend. Elements whose keys are equal keep their order. An element that is not a position inkeysstops the program, even in a list of one element (control-flow.md§5.2).
text makes a string primitive from a list of bytes (types.md §2.12):
@primitives$String text(this @primitives$List<T Type>)
The string’s bytes are the list’s elements, in order.
These methods are generic so that they take the lists core declares, whose elements are core‘s own types, but each works on its elements’ storage and accepts only some element types. A wrapper of a type is a struct whose only field is of that type or is a wrapper of it, as core’s Int wraps @primitives$I64. A call with any other element type is a compile-time error:
| Methods | Element type |
|---|---|
appendRange, appendOwn, copyRange |
Any type that owns no block: no @primitives$String, list primitive, @primitives$ByteSet, or boxed member (adt.md §4) at any depth |
sameRange, hashRange |
@primitives$I32, @primitives$I64, @primitives$U8, @primitives$Bool, or a wrapper of one of them |
sort |
@primitives$I32, @primitives$I64, @primitives$U8, or a wrapper of one of them, compared as integers |
sortBy |
The list’s elements are @primitives$I32 or a wrapper of it, and the keys @primitives$I64 or a wrapper of it |
text |
@primitives$U8 or a wrapper of it |
row @primitives$List<@primitives$I64> = @primitives$List(@primitives$I64);
row!push(@primitives$I64(4));
row!push(@primitives$I64(1));
row!appendOwn(@primitives$I64(1), @primitives$I64(2)); // 4, 1, 4, 1
row!sort(); // 1, 1, 4, 4
same @primitives$Bool = row:sameRange(@primitives$I64(1), row, @primitives$I64(2), @primitives$I64(1)); // true
Story:
stories/generics.md— “The list primitives’ operations are specified, and a position outside a list stops the program”.
9. Deferred Features
The following are intentionally not specified in this version:
- arithmetic on number parameters in type positions (for example
Array<T, rows * cols>), pending a type-level equality rule for such expressions - map types — the map literal form is specified (
syntax.md§2.10), but a map type, whether a map preserves any order, and what a map requires of a key type are all open - named lane access (
.x,.y,.z,.w) - phantom type parameters — an introduced parameter (a type’s header parameter, or a verb’s inline parameter) with no path from any value argument, subject, or literal that fixes it
- generic function values — a function value that is itself polymorphic over type or number parameters; the open question is runtime representation (monomorphization versus dictionary passing), not overload resolution or type checking, since a generic function type is a unique parameter shape (see
functions.md§7.6)
Story:
stories/generics.md— “Deferred: what the model promises but does not yet deliver” records why each item is open, including the constraints/bounds gap and the type-level equality problem.
10. Summary
| Concept | Rule |
|---|---|
| Type as template | A type definition lists parameters in a <> header and produces a result; applying arguments evaluates a type into a concrete type |
| Growing-argument rule | A type that leads back to itself, through any member or other type, passes back each argument as one of its own parameters or as one naming none; passing a parameter inside a larger argument is an error at the declaration |
| Type parameter | Declared name Type with an uppercase name (T); ranges over types |
| Number parameter | Declared name @concepts$Int with a lowercase name (n); ranges over compile-time integers and resolves to a number value in body positions |
Type / @concepts$Int |
Compiler concept types; legal only in parameter positions, never as storage; the only two concept types a type’s or a package’s <> header holds |
| Reference | A parameter is referenced by bare name; casing separates types from numbers. A type’s or a package’s header, or a verb’s inline concept, marks an identifier as a parameter |
| Type expression | Type<arg, ...>; a compile-time structural description; used in fields, signatures, returns, aliases, and nested arguments |
| Call | Type(arg, ...); a runtime construction or function call; always by bare name; never takes a <> list |
| Inferred parameter | Introduced inline on a verb parameter’s type, in a nested type, or in a slot of a verb parameter’s function type; deduced from the value arguments at the call |
| Type-parameter subject | this S Type introduces S as the subject’s whole type, so the method applies to a subject of any type; the subject is a veiled value; lookup reaches it through the spelling the file has for its package |
| Veiled value | A value whose type is a type parameter; a generic body holds it, calls methods, functions, operators and constructors on it, and reads its subscripts; a field read, enum map read, enum member named without a call (T.red), match, or call as a function value on it is an error where the body writes it; T.name(...) is always a named-constructor call; a missing verb is reported at the type’s origin |
| Explicit parameter | Declared as a Type/@concepts$Int value parameter in (); passed positionally (Vector(Int), Array(Int, 10000)) |
| Concept-typed literal | Must be wrapped in its destination type before driving inference |
| Wrong-kind type argument | A type that cannot fill the slot its parameter reaches, such as a reference type in a value mould’s field, is reported at its origin: the explicit argument, or the value argument an inferred type is read from; the diagnostic names the path from there to the rejecting slot |
@primitives$Array<T, n> |
Fixed-size value-type storage primitive: n contiguous elements of type T; core declares Array over it |
@primitives$ArrayRef<T, n> |
Fixed-size reference-type storage primitive over any T, with Array’s layout; built from a literal by ArrayRef, which borrows each element and so copies values and & values, by ArrayRef.roaming, which takes owners, or by ArrayRef.refs, which mints or copies references and may mix the two in one literal, or by fill, whose lambda takes each position as @primitives$I64 or a type with an implicit constructor from it; its elements take their root’s state, may be referenced under a settled root, and are never moved out; core declares ArrayRef over it |
@primitives$List<T> |
Dynamically sized value-type storage primitive over a value T: elements in the dynamic region behind a fixed-size handle; a copy copies every element; core declares List over it |
@primitives$ListRef<T> |
Dynamically sized reference-type storage primitive with List’s layout; its elements are owners or values, never &, and roaming; core declares ListRef over it |
| Container operations | Each container primitive has a subscript counted from 1; @primitives$List(T) and @primitives$ListRef(T) build an empty list, and push, size, remove, and swapRemove grow, measure, and shrink it; a position outside stops the program |
| Range operations | A List has appendRange, appendOwn, copyRange, sameRange, hashRange, sort, sortBy, and text; each is generic but accepts only the element types its storage allows, a struct wrapping one of them included, and a range outside the list stops the program |
| Size in the type | Required for uniform stride and therefore for cheap indexing, copying, embedding, and calls |