Generics
v0.2Generic functions, classes, structs, enums, specialization, inference, and bounds.
On this page
PrinciPL 0.2 supports generic functions, classes, structs, and enums. The compiler specializes a declaration for concrete type arguments at build time, so generic calls use statically checked native code.
#Generic functions
Declare type parameters between the function name and parameter list. Calls may specify type arguments, or infer them from the actual argument types when every parameter can be determined:
fn identity<T>(value: T) -> T {
return value
}
fn main() {
let number = identity<Int>(42)
let text = identity("hello") // infers T = String
println(number)
println(text)
}
Inference uses call arguments only; it does not use the expected result type. If a type parameter cannot be resolved from arguments, provide it explicitly. A generic function compiles only for concrete specializations used by the program. Repeated equivalent calls share one specialization; recursive calls with the same types are supported. Expanding recursion is diagnosed rather than allowed to specialize indefinitely.
The backend caches specializations by function and concrete type arguments, so
identity<Int>(1) and identity<Int>(2) share generated code while
identity<String>("one") uses a separate specialization. Internal names are
deterministic compiler details. Each build is limited to 512 distinct generic
function specializations and 512 generic type layouts; exceeding either limit
produces a diagnostic.
#Generic classes and structs
Generic type arguments are explicit at type references and construction sites. Constructor inference is available only when its parameter types determine all type arguments:
class Box<T> {
value: T
init(value: T) {
self.value = value
}
fn get() -> T {
return self.value
}
}
struct Pair<A, B> {
first: A
second: B
}
fn main() {
let box = Box<Int>(42)
let pair = Pair<Int, String>(1, "one")
println(box.get())
println(pair.second)
}
The same type syntax nests: Box<List<Int>>, List<Box<String>>, and Pair<Box<Int>, List<Float>>. Class specializations retain reference semantics; struct specializations retain value semantics. Generic enums use explicit concrete type arguments when constructing variants, for example Maybe<Int>.Some(3).
#Bounds
Generic functions, classes, and structs can require declared traits or interfaces. Use + for multiple bounds. Every bound must be satisfied by the same concrete type argument:
trait Named {
fn name() -> String
}
interface Drawable {
fn draw() -> String
}
fn describe<T: Named + Drawable>(value: T) -> String {
return value.name() + ":" + value.draw()
}
class User implements Named, Drawable {
value: String
init(value: String) {
self.value = value
}
fn name() -> String { return self.value }
fn draw() -> String { return "portrait" }
}
fn main() {
println(describe(User("Ada")))
}
At specialization, each concrete argument must implement every required contract. A generic parameter can call only methods guaranteed by its bounds. Interface bounds require a conforming class; trait bounds accept conforming classes or structs.
Bounds can also be declared on generic class and struct types:
class Repository<T: Named> {
value: T
init(value: T) {
self.value = value
}
}
struct NamedValue<T: Named> {
value: T
}
#Current v0.2 limitations
Methods cannot declare their own type parameters; methods on a generic class can use that class's parameters. Traits and interfaces cannot be generic and bounds cannot take type arguments. Generic function inference does not use a result context, and enum variant construction requires explicit generic arguments. Generic code is monomorphized rather than runtime-erased. There is no runtime generic reflection, user-written specialization syntax, associated type, higher-kinded type, or variadic generic.