// Currying (ADR 0061): partial application is a value, labels apply by // name in any order, pipes stay x-first. import std/list fn add :: a: Int b: Int -> Int { a + b } fn insert :: ~into: String ~key: String value: String -> String { "{into}:{key}:{value}" } // A partial application is a function of the remaining parameters. pub fn add2 :: -> fn(Int) -> Int { add 2 } // Labels partially applied by name, out of order. pub fn keyed :: value: String -> fn(~into: String) -> String { insert ~key: "k" value } pub fn by_label :: -> String { let into_users = insert ~into: "users" into_users ~key: "k" "v" } // A pipe is `f x a b` (x-first): currying through it. pub fn through_pipes :: xs: List -> List { xs |> list.map (add 1) |> list.map (add 2) } // Too many arguments is the arity error left (E0205). pub fn too_many :: -> Int { add 1 2 3 //~ ERROR E0205 } // A label no parameter has (E0204), in a partial application. pub fn no_such_label :: -> String { insert ~nope: "x" "v" //~ ERROR E0204 } // A constructor is not a function: no partial construction. type User { User(name: String, age: Int) } pub fn partial_ctor :: -> User { User("ada") //~ ERROR E0205 } // A constructor applied like a function. pub fn ctor_applied :: -> User { User "ada" //~ ERROR E0216 } // A construction's parentheses touch the constructor: with a space, they // are an argument (issue #319). pub fn spaced_construction :: -> Option { Some (1) //~ ERROR E0216 } // A partial application takes its handlers as the lambda `fn n { f a n }` // would in its place (ADR 0062, ADR 0010): passed where the parameter's // type declares the effect, it receives the handler from its caller... effect log { fn log :: msg: String -> () } fn logs :: prefix: String n: Int -> String / { log prefix prefix <> show n } fn run :: g: fn(Int) -> String / -> String { handle { g 1 } with { log _ -> () } } pub fn passed :: -> String { run (logs "#") } // ...and stored in a `let`, it uses the handlers in scope where it is // written, so it cannot be used inside a `handle` it was made outside of. pub fn stored_outside :: -> List { let g = logs "#" handle { list.map [1] g //~ ERROR E0703 } with { log _ -> () } } // A partial application holds its callee and the arguments it was given, // as the lambda `fn n { f a n }` would: it can be sent when they can // (issue #325)... fn keep :: f: send fn(Int) -> Int -> Int { f 1 } fn apply :: g: fn(Int) -> Int n: Int -> Int { g n } type Offset { Offset(by: Int) } pub fn sent :: n: Int -> Int { keep (add n) } pub fn sent_field :: offset: Offset -> Int { keep (add offset.by) } pub fn sent_later :: n: Int -> Int { let g = add n keep g } pub fn sent_callee :: g: send fn(Int, Int) -> Int -> Int { keep (g 1) } // ...and not when it holds what cannot be: a plain `fn`, as an argument // or as the callee. pub fn holds_a_plain_fn :: g: fn(Int) -> Int -> Int { keep (apply g) //~ ERROR E0704 } pub fn plain_callee :: g: fn(Int, Int) -> Int -> Int { keep (g 1) //~ ERROR E0704 } // A handler its callers supply is received, not captured. fn keep_logged :: f: send fn(Int) -> String / -> String / { f 1 } pub fn sent_with_its_handler :: -> String / { keep_logged (logs "#") }