// Associated types (ADR 0047): a trait names a type each impl chooses, // written `Elem`, computed once the impl is known. import std/io import std/list import std/process.{type Subject, type proc} pub trait Container { type Elem fn to_list :: c: c -> List> fn first :: c: c -> Option> } impl Container> { type Elem = a fn to_list :: c { c } fn first :: c { match c { [x, ..] -> Some(x), [] -> None } } } pub type Pair { Pair(Int, Int) } impl Container { type Elem = Int fn to_list :: p { match p { Pair(a, b) -> [a, b] } } fn first :: p { match p { Pair(a, _) -> Some(a) } } } // A container of containers: its elements are its parts' elements. pub type Nested { Nested(List) } impl Container> where Container { type Elem = Elem fn to_list :: n { match n { Nested(parts) -> list.flat_map parts to_list } } fn first :: n { match to_list n { [x, ..] -> Some(x), [] -> None } } } // Generic code: the element type is computed from the container's. fn count :: c: c -> Int where Container { list.length (to_list c) } fn describe :: c: c -> String where Container, Show> { "{count c} items, first {first c}" } // Inferred: `fn(a) -> Option> where Container`. fn first_of :: c { first c } // A field whose type is a projection: `Boxed(41)` is checked once the // expected type says which container. pub type Boxed { Boxed(Elem) } fn unbox :: b: Boxed -> Int { match b { Boxed(n) -> n + 1 } } // Inferred with an equality: `fn(a) -> Option where Container`. fn first_int :: c { let x: Option = first c x } // Bindings: in the body, `Elem` is `Int`; each call must agree. fn total :: c: c -> Int where Container { list.fold (to_list c) 0 fn a x { a + x } } // A binding to a variable, used by another constraint. fn show_all :: c: c -> String where Container, Show { show (to_list c) } // A constructor that knows its container holds Ints: its arm does too. type Summable { Summable(c) -> Summable where Container } fn sum_any :: s: Summable -> Int { match s { Summable(c) -> total c } } // Generic associated types: own parameters after the trait's. pub trait Mappable { type Mapped fn map_all :: c: c f: fn(Int) -> x -> Mapped } impl Mappable { type Mapped = (x, x) fn map_all :: p f { match p { Pair(a, b) -> (f a, f b) } } } pub fn main :: -> () / { io.println (describe [1, 2, 3]) io.println (describe Pair(4, 5)) io.println (describe Nested([[6], [7, 8]])) io.println (show (first_of Pair(9, 10))) io.println (show (unbox Boxed(41))) io.println (show (first_int [12])) io.println (show (total [1, 2, 3] + total Pair(4, 5))) io.println (show_all ["x", "y"]) io.println (show [sum_any Summable(Pair(6, 7)), sum_any Summable(Nested([[8]]))]) let (a, b) = map_all Pair(2, 3) fn n { "{n * n}" } io.println (a <> " " <> b) // A message whose type is a projection is checked like any other. let inbox: Subject>> = process.subject () process.send inbox "sent" io.println (process.receive_forever inbox) } //> 3 items, first Some(1) //> 2 items, first Some(4) //> 3 items, first Some(6) //> Some(9) //> 42 //> Some(12) //> 15 //> [x, y] //> [13, 8] //> 4 9 //> sent