// Generic associated types (ADR 0047): an associated type with parameters // of its own, and bindings that quantify them. import std/io import std/list pub trait Mappable { type Elem type Mapped fn map_all :: c: c f: fn(Elem) -> x -> Mapped } impl Mappable> { type Elem = a type Mapped = List fn map_all :: c f { list.map c f } } pub type Pair { Pair(Int, Int) } impl Mappable { type Elem = Int type Mapped = (x, x) fn map_all :: p f { match p { Pair(a, b) -> (f a, f b) } } } // Whatever `c` is, mapping it gives a list: the body may rely on it. fn lengths :: c: c -> Int where Mappable = List> { list.length (map_all c fn s { s }) } fn doubled :: c: c -> List where Mappable = List> { map_all c fn n { n * 2 } } pub fn main :: -> () / { io.println (show (lengths ["a", "bb", "ccc"])) io.println (show (doubled [1, 2])) let (a, b) = map_all Pair(3, 4) fn n { n + 1 } io.println (show [a, b]) } //> 3 //> [2, 4] //> [4, 5]