Generics
Generics allow writing code that works with any type.
Generic Actions
Section titled “Generic Actions”action identity<T>(@value: T) -> T return @valuefin action.
@identity(42) @// Int@identity("hello") @// StringMultiple Type Parameters
Section titled “Multiple Type Parameters”action pair<A, B>(@first: A, @second: B) -> (A, B) return (@first, @second)fin action.
@result = pair(1, "one")Generic Records
Section titled “Generic Records”record Container<T> value: Tfin record.
@int_container = Container<Int> { value: 42 }@str_container = Container<String> { value: "hello" }Generic Enums
Section titled “Generic Enums”enum Option<T> some(value: T) nonefin enum.
enum Result<T, E> ok(value: T) err(error: E)fin enum.Type Constraints
Section titled “Type Constraints”Constrain generics to specific types:
action print<T: Display>(@value: T) write(@value#to_string())fin action.Multiple Constraints
Section titled “Multiple Constraints”action process<T: Display + Clone>(@value: T) @copy = @value#clone() write(@copy#to_string())fin action.Trait Bounds
Section titled “Trait Bounds”action sort<T: Comparable>(@items: Array<T>) -> Array<T> return @items#sort()fin action.Where Clauses
Section titled “Where Clauses”Complex constraints with where:
action process<T, U>(@input: T) -> U where T: Display + Clone U: From<T> return U::from(@input)fin action.Generic Implementation
Section titled “Generic Implementation”record Pair<A, B> first: A second: Bfin record.
impl<A, B> Pair<A, B> action swap(@self: Pair<A, B>) -> Pair<B, A> return Pair { first: @self#second, second: @self#first } action fin.fin impl.Constrained Implementation
Section titled “Constrained Implementation”impl<T: Display> Container<T> action show(@self: Container<T>) write(@self#value#to_string()) fin action.fin impl.Associated Types
Section titled “Associated Types”trait Iterator type Item action next(@self: mut Iterator) -> Option<Self::Item>fin trait.
impl Iterator for Counter type Item = Int action next(@self: mut Counter) -> Option<Int> if @self#current < @self#max @value = @self#current @self#current = @self#current + 1 return some(@value) fin if. return none fin action.fin impl.Phantom Types
Section titled “Phantom Types”record Tagged<T, Tag> value: Tfin record.
type Meters = Tagged<Float, "meters">type Feet = Tagged<Float, "feet">Generic Type Inference
Section titled “Generic Type Inference”Draft infers generic types from usage:
@result = identity(42) @// T inferred as Int@result = pair(1, "one") @// A inferred as Int, B as StringCommon Generic Patterns
Section titled “Common Generic Patterns”Option
Section titled “Option”enum Option<T> some(value: T) nonefin enum.Result
Section titled “Result”enum Result<T, E> ok(value: T) err(error: E)fin enum.Vec (Dynamic Array)
Section titled “Vec (Dynamic Array)”record Vec<T> data: *T length: Int capacity: Intfin record.HashMap
Section titled “HashMap”record HashMap<K, V> buckets: Array<Array<(K, V)>> size: Intfin record.Generic Performance
Section titled “Generic Performance”Generics in Draft use monomorphization. Each concrete type generates specialized code at compile time with zero runtime overhead.
