Generics
This page covers types and functions that take type parameters or number parameters, and the container types built on them.
Generic types
A type can take parameters in < > after its name. Type marks a parameter
that ranges over types:
package hello;
import core$;
type Pair<A Type, B Type> = struct {
first A;
second B;
}
Pair<A, B>(first A Type, second B Type) => init{ first; second; }
B second(this Pair<A Type, B Type>) => this.second;
Unit main() {
console Console(@program$console);
p Pair(String("answer"), Int(42));
console!print(p.first + " = " + String(p:second()));
q Pair<Int, Bool> = Pair(Int(1), Bool(true));
console!print(String(q.first));
return Unit();
}
answer = 42
1
Pair<String, Int> and Pair<Int, Bool> are two separate concrete types,
each with its own fixed layout.
A function, method or constructor has no < > header of its own. Instead, a
type parameter is introduced where it first appears with its concept:
first A Type introduces A, and after that A is written bare. A call has
no < > either: Pair(String("answer"), Int(42)) infers A and B
from the arguments.
Generic functions
The compiler checks a generic function separately for each type it is called with, so the function’s body can use any operation that those types have:
package hello;
import core$;
T largest(values List<T Type>) {
best T = values[Int(1)];
i Int(2);
i!to(values:size()) {
if(best < values[i]) {
best = values[i];
}
}
return best;
}
Unit main() {
console Console(@program$console);
ints List<Int> = List(Int);
ints!push(Int(4));
ints!push(Int(9));
ints!push(Int(2));
console!print(String(largest(ints)));
floats List<Float> = List(Float);
floats!push(Float(0.5));
floats!push(Float(1.5));
console!print(String(largest(floats)));
return Unit();
}
9
1.5
Calling largest with a list whose element type has no < is a
compile-time error, and the error message names both the missing < and the
call that needs it.
Passing a type as an argument
A parameter of type Type takes a type as an ordinary argument. That is how
an empty container is made: List(Int) is an empty List<Int>, because an
empty list has no elements from which the constructor could infer the element
type.
package hello;
import core$;
type Stack<T Type> = struct {
items List<T>;
}
Stack<T>(T Type) => init{ items = List(T); }
Unit push(this Stack<T Type>, item T) mut {
this.items!push(item);
return Unit();
}
T pop(this Stack<T Type>) mut => this.items!remove(this.items:size());
Int size(this Stack<T Type>) => this.items:size();
Unit main() {
console Console(@program$console);
s Stack(String);
s!push("first");
s!push("second");
top String = s!pop();
console!print(top + ", " + String(s:size()) + " left");
return Unit();
}
second, 1 left
Number parameters
A parameter written n @concepts$Int ranges over whole numbers known at
compile time. @concepts$Int is the type of an integer literal
(Intrinsics and concepts). Array<T, n> uses a number parameter for its length, so the
length is part of the array’s type:
package hello;
import core$;
Int total(values Array<Int, n @concepts$Int>) {
sum Int(0);
i Int(1);
i!to(values:size()) {
sum = sum + values[i];
}
return sum;
}
Unit main() {
console Console(@program$console);
three Array<Int, 3> = Array([Int(1), Int(2), Int(3)]);
five Array<Int, 5> = Array([Int(1), Int(1), Int(1), Int(1), Int(1)]);
console!print(String(total(three)) + " " + String(total(five)));
return Unit();
}
6 5
Containers
core has four containers, all generic over their element type:
| Type | Size | Elements |
|---|---|---|
Array<T, n> |
fixed, n |
values, copied with the array |
List<T> |
grows | values, copied with the list |
ArrayRef<T, n> |
fixed, n |
elements that stay in place; the container is a reference type |
ListRef<T> |
grows | owners, moved in and out |
List and Array are value types themselves: assigning a list copies it
with its elements. ListRef holds owners of reference types, which
Memory explains. The core
library pages list
the containers’ methods.
In the spec
- Generics: types as templated functions, the parameter model, calls and container primitives
- The story behind it: generics