// Associated types (ADR 0047): declarations, definitions and their rules. import std/list import std/process.{type Subject, type proc} import std/ref.{type Ref} pub trait Container { type Elem fn first :: c: c -> Option> } impl Container> { type Elem = a fn first :: c { match c { [x, ..] -> Some(x), [] -> None } } } // Inferred through projections. fn first_of :: c { first c } //~ TYPE first_of: fn(a) -> Option> where Container // Definitions: each impl defines each, once, with its own variables. pub type One { One } pub type Two { Two } pub type Three { Three(a) } pub type Four { Four(a) } impl Container //~ ERROR E0407 must define associated type `Elem` { fn first :: _ { None } } impl Container { type Elem = Int type Extra = Int //~ ERROR E0406 not an associated type fn first :: _ { None } } impl Container> { type Elem = b //~ ERROR E0211 type variable `b` fn first :: _ { None } } // Reduction must end: a definition's projections take the impl's variables. impl Container> { type Elem = Elem> //~ ERROR E0415 might never be computed fn first :: _ { None } } pub trait Defaulted { type Elem2 = Int //~ ERROR E0120 defined by each impl fn get :: c: c -> Elem2 } impl Container { type Elem //~ ERROR E0120 must define associated type `Elem` fn first :: _ { None } } // A projection needs its trait, and does not determine its argument. fn unproved :: //~ ERROR E0402 `Container` is not known to hold c: c -> Option> { None } fn undetermined :: //~ ERROR E0403 associated types -> Option> where Container { None } // Concrete arguments: the impl decides. fn concrete :: x: Elem> -> Int { x + 1 } // Unknown arguments: the equality waits for them. In an inferred // function it becomes part of its type. fn forced :: c { let x: Option = first c x } //~ TYPE forced: fn(a) -> Option where Container fn forced_ok :: -> Option { forced [1] } fn forced_wrong :: -> Option { forced ["one"] //~ ERROR E0202 type mismatch } // In an annotated function nothing determines `x`'s type. fn undecided :: -> Int { let vs = [] let a = match vs { [v, ..] -> first v //~ ERROR E0403 which type should implement `Container` , [] -> None } let x: Option = a //~ ERROR E0403 cannot tell what 0 } // Each equality is decided under its own arm's equalities: `Elem` is // `Int` in one arm and `String` in the other. type Kind { Ints -> Kind>, Texts -> Kind> } fn per_arm :: k: Kind x: r -> Int where Container { let vs = [] let a = match vs { [v, ..] -> first v, [] -> None } let n = match k { Ints -> { let i: Option = a 0 } , Texts -> { let s: Option = a 1 } } let _ = [x, ..vs] n } fn per_arm_wrong :: k: Kind x: r -> Int where Container { let vs = [] let a = match vs { [v, ..] -> first v, [] -> None } let n = match k { Ints -> { let i: Option = a //~ ERROR E0202 type mismatch 0 } , Texts -> { let s: Option = a 1 } } let _ = [x, ..vs] n } // Bindings: what a projection is, in a `where` clause. fn total :: c: c -> Int where Container { 0 } fn total_ok :: -> Int { total [1, 2] } fn total_wrong :: -> Int { total ["one"] //~ ERROR E0202 type mismatch } fn bound_var :: c: c -> Option where Container { first c } fn unbound_var :: -> Option where Container //~^ ERROR E0403 only in the `where` clause { None } fn cyclic :: c: c -> Int where Container>> //~^ ERROR E0416 refers to itself { 0 } fn twice :: //~ ERROR E0416 bound twice c: c -> Int where Container, Container { 0 } fn against_impl :: x: Int -> Int where Container, Elem = String> //~^ ERROR E0416 by its impl { x } fn unknown_name :: c: c -> Int where Container //~^ ERROR E0406 no associated type `Size` { 0 } fn misplaced :: x: Option -> Int //~^ ERROR E0416 can only follow a trait's arguments { 0 } // Quantified bindings of generic associated types hold for every type. pub trait Mappable { type Mapped fn map_all :: c: c f: fn(Int) -> x -> Mapped } pub type Duo { Duo(Int, Int) } impl Mappable { type Mapped = (x, x) fn map_all :: d f { match d { Duo(a, b) -> (f a, f b) } } } pub type IntList { IntList(List) } impl Mappable { type Mapped = List fn map_all :: c f { match c { IntList(ns) -> list.map ns f } } } fn needs_list :: c: c -> Int where Mappable = List> { 0 } //~ TYPE needs_list: fn(c) -> Int where Mappable = List> fn needs_list_ok :: -> Int { needs_list IntList([1]) } fn needs_list_wrong :: -> Int { needs_list //~ ERROR E0202 type mismatch Duo(1, 2) } fn shadowing :: c: c y: y -> Int where Mappable = List> //~^ ERROR E0416 already names a type variable { 0 } // Not an index of a constructor, and no derive. type Indexed { ByElem(Int) -> Indexed< //~ ERROR E0214 cannot mention an associated type Elem> > } @derivable count trait Sized2 //~ ERROR E0412 has associated types { type Unit fn size :: x: a -> Int } fn count :: c: Constructor -> Int { 0 } // `Send` looks through projections. pub trait Holds { type Held fn held :: c: c -> Held } pub type Cell { Cell(Ref) } impl Holds> { type Held = Ref fn held :: c { match c { Cell(r) -> r } } } fn sends_held :: c: Cell to: Subject>> -> () / { process.send to (held c) //~ ERROR E0704 cannot be sent } fn sends_generic :: c: c to: Subject> -> () / where Holds { process.send //~ ERROR E0402 `Held` is not known to implement `Send` to (held c) } fn sends_bounded :: c: c to: Subject> -> () / where Holds, Send> { process.send to (held c) }