Data & Control

Pipelines & Placeholders

Pass results between calls and choose the argument positions that receive them with the placeholder _.

Connect function calls with |> and trace how each result becomes the next input. Use the placeholder _ to choose which argument receives that input.

Install Topaz using First Program. For function declarations and calls, see Values and Functions.

Follow a value through three calls

Save this code as pipelines-placeholders.tpz in your working folder.

TOPAZ
function add(value: int, amount: int) -> int {
    value + amount
}

function multiply(left: int, right: int) -> int {
    left * right
}

function equal(left: int, right: int) -> bool {
    left == right
}

let result = 5
    |> add(2)
    |> multiply(_, 3)
let repeated = result |> equal(_, _)

print("{result} {repeated}")

In that folder, check the file and run it.

BASH
topaz check pipelines-placeholders.tpz
topaz run pipelines-placeholders.tpz

The check succeeds with exit code 0. The program prints these results.

Output
21 true

Trace the result

StageCall after passing the valueResult
5 |> add(2)add(5, 2)7
7 |> multiply(_, 3)multiply(7, 3)21
21 |> equal(_, _)equal(21, 21)true

Pipelines run from left to right. Each stage evaluates its left side once, before anything on the right. Both placeholders in equal(_, _) receive that same saved value.

Choose where the input goes

Right sideWhat Topaz does
value |> call(other)Inserts value as the first argument
value |> call(_, other)Replaces each valid _ with value
value |> callableCalls the function value with value as its argument
value |> .fieldReads field from value

Without a placeholder, the inserted argument comes before positional, spread, and named arguments. With placeholders, Topaz replaces them and adds no extra first argument.

A placeholder belongs to its nearest enclosing call. Several placeholders can receive the saved input. In a pattern, _ instead matches a value without binding a name.

Correct an argument position

In pipelines-placeholders.tpz, removing _ from multiply(_, 3) leaves multiply(3). The pipeline still inserts the previous result first and produces 21. Writing multiply(7, 3) there would supply three arguments after insertion and fail the check.

Put _ inside the receiving call's argument list. It cannot stand alone on the right or appear as a callee, member name, or field label.

Named arguments must follow positional and spread arguments. A right side that is neither a call, a function value, nor .field is a static error.

For an optional field, use ordinary optional chaining before the pipeline or inside a lambda. The form value |> ?.field is unsupported.

Handle errors and concurrency explicitly

Use postfix ? to propagate a Result and concurrent with a declared timeout for concurrent work. Pipelines do not automatically propagate errors, create partially applied functions, or start asynchronous work. User-defined pipe operators are unsupported.

See Operators & Expressions for precedence and Functions & Generics for function values. Collections & Comprehensions explains stages that produce collections.