import std/io import std/list // `_` placeholders: pipes and captures (ADR 0023). type Point { Point(x: Int, y: Int) } fn add :: x: Int y: Int -> Int { x + y } fn add3 :: x: Int y: Int z: Int -> Int { x + y + z } // A capture is a one-parameter function, typed like the `fn` it stands for. pub fn increment :: -> fn(Int) -> Int { add 1 _ } fn captured :: { add 1 _ } //~^ TYPE captured: fn() -> fn(Int) -> Int / fn written_out :: { fn v { add 1 v } } //~^ TYPE written_out: fn() -> fn(Int) -> Int / // Its effects are the call's, performed when it is called. pub fn print_all :: xs: List -> () / { list.each xs (io.println _) } pub fn print_none :: xs: List -> () { list.each xs (io.println _) //~ ERROR E0502 } pub fn placed :: xs: List -> List { xs |> list.map (add 10 _) } pub fn labelled :: y: Int -> Point { y |> Point(x: 0, y: _) } // A wrong placement is a type error, as for any argument. pub fn misplaced :: s: String -> Int { s |> add 1 _ //~ ERROR E0202 } fn two_holes :: { add3 1 _ _ //~ ERROR E0116 only one `_` } pub fn piped_two_holes :: x: Int -> Int { x |> add3 _ 1 _ //~ ERROR E0116 only one `_` } pub fn with_spread :: p: Point -> Point { 1 |> Point(x: _, ..p) //~ ERROR E0116 spread } pub fn not_an_argument :: -> Int { let x = _ //~ ERROR E0116 call's arguments x } pub fn in_a_list :: -> List { [1, _] //~ ERROR E0116 call's arguments } pub fn in_an_operator :: -> Int { 1 + _ //~ ERROR E0116 call's arguments } pub fn piped_into_hole :: x: Int -> Int { x |> _ //~ ERROR E0116 call's arguments } pub fn in_use :: xs: List -> () / { use x <- list.each _ //~ ERROR E0116 `use` io.println x } // A capture's arity is its callee's: here one argument too many. fn wrong_arity :: { add 1 2 _ //~ ERROR E0205 }