Pipes
Ferret’s pipe operator (|>) passes the left-hand value into a function call on the right-hand side.
Basic Usage
Section titled “Basic Usage”By default, the value is appended as the first argument:
import "std/io";
fn add(x: i32, y: i32) -> i32 { return x + y; }
let result := 5 |> add(10); // add(5, 10)io::Println(result);Placeholder _
Section titled “Placeholder _”Use _ to control where the piped value goes:
import "std/io";
fn add(x: i32, y: i32) -> i32 { return x + y; }
let result := 5 |> add(10, _); // add(10, 5)io::Println(result);Only one placeholder is allowed in a pipe stage.
Chaining
Section titled “Chaining”Pipes compose cleanly:
fn add(x: i32, y: i32) -> i32 { return x + y; }fn mul(x: i32, y: i32) -> i32 { return x * y; }
let result := 5 |> add(10) |> mul(2); // mul(add(10, 5), 2)Optional Parentheses
Section titled “Optional Parentheses”If you are not passing any extra arguments, the function call can omit parentheses:
fn incr(v: i32) -> i32 { return v + 1; }
let x := 0 |> incr |> incr |> incr;Once you pass additional arguments, you must use parentheses:
let value := 5 |> add(10); // oklet other := 5 |> add(_, 10); // ok// let bad := 5 |> add 10; // invalidType Rules
Section titled “Type Rules”- The right-hand side must be a callable (function or function value).
- The piped value must type-check against the target parameter (or placeholder position).
- Only one placeholder (
_) is allowed per pipe stage. - You cannot pipe a
voidvalue into the next stage.