The pipeline operator |>
$value |> f($_) is the same as f($value). $_ is the place where the value is used.
|>
Passes a value through several steps, written from left to right in the order they run.
You write the value, then |>, then the step. Inside the step, $_ is the place where the value goes. Each step takes the result of the step before it. To trim a string and then convert it to lower case, you write two steps.
Without |> you write one call inside another, and you read the steps from the inside to the outside. Both forms do the same work, and |> adds no cost when the program runs.
Good to know: each step needs exactly one $_. A step with none, or with two, does not compile. The right side of |> is the call itself, with $_ in it.
The examples below clean up a form field in two steps, build a URL slug in five steps and tidy a list of tags. Example 04 uses |> with other operators. Examples 05 and 06 do not compile.
Clean up a form field
<?nvs// Shows how to clean up an email address from a signup form in two steps.// `|>` gives the value on its left to the step on its right. `$_` is the place// where the value goes. The steps run from left to right.// The first address prints as ada@example.com.
array<string> $submitted = [' Ada@Example.COM ', 'BOB@example.com', ' carol@Example.Com '];
foreach ($submitted as string $raw) { string $email = $raw |> Core\Str::trim($_) |> Core\Str::lower($_); echo $email, "\n";}ada@example.combob@example.comcarol@example.comMake a url slug
<?nvs// Shows how to make the slug for a URL from the title of an article, in five steps.// Each step is on a line of its own, and the steps run from the top to the bottom.// The first title prints as hello-world.
array<string> $titles = ['Hello World', ' Novis Ships 1.0 ', 'Web Servers, Fast'];
foreach ($titles as string $title) { string $slug = $title |> Core\Str::trim($_) |> Core\Str::lower($_) |> Core\Str::replace($_, ',', '') |> Core\Str::replace($_, ' ', ' ') |> Core\Str::replace($_, ' ', '-'); echo $slug, "\n";}hello-worldnovis-ships-1.0web-servers-fastTidy a tag field
<?nvs// Shows how to tidy the tags that a person typed into one form field, separated by commas.// The steps split the text, trim each tag, delete the duplicates and sort the tags.
string $field = 'web, cli , web, jit ,security';
array<string> $tags = $field |> Core\Str::split($_, ',') |> Core\Arr::map($_, fn(string $t): string => Core\Str::trim($t)) |> Core\Arr::unique($_) |> Core\Arr::sort($_);
// `$tags` is a normal variable, so a new pipeline can start from it.// This prints cli,jit,security,web and then 4 tags.echo $tags |> Core\Str::join($_, ','), "\n";echo $tags |> Core\Arr::count($_), " tags\n";cli,jit,security,web4 tagsPipelines beside other operators
<?nvs// Shows how `|>` works together with the operators around it.
string $s = ' Novis ';echo $s |> Core\Str::trim($_) |> Core\Str::lower($_), "\n";
// A prefix `-` runs first, so `Core\Math::abs` gets 7. `|>` runs before `>`,// so the second value compares the length with 5. This prints 7 1.int $n = -7;echo -$n |> Core\Math::abs($_), ' ', $s |> Core\Str::length($_) > 5, "\n";
// The right side may be any expression in brackets. This prints a-b-c 10.array<string> $xs = ['c', 'a', 'b'];echo $xs |> Core\Arr::sort($_) |> Core\Str::join($_, '-'), ' ', 5 |> ($_ * 2), "\n";novis7 1a-b-c 10A step without the hole
<?nvs// Shows that a method reference is not a step. The right side must be a call with `$_` in it.// proof: compile-error
string $s = "hi";echo $s |> Core\Str::upper, "\n";error[E0129]: the right side of `|>` needs the hole `$_` --> 05-a-step-without-the-hole.nvs:6:12 |6 | echo $s |> Core\Str::upper, "\n"; | ^^^^^^^^^^^^^^^ no `$_` here, so the piped value has nowhere to go = help: write the hole where the value goes — `Str::trim($_)` = note: PHP 8.5's `|>` applies a callable; Novis's substitutes `$_` at compile time
error: aborting due to 1 errorTo read what a code means and how to fix it, run `nvs agent show <code>`.The hole twice and outside a pipeline
<?nvs// Shows the two other places where `$_` does not compile.// The first step has `$_` twice. The second line uses `$_` outside a pipeline.// proof: compile-error
string $s = "hi";echo $s |> Core\Str::replace($_, $_, "x"), "\n";string $t = $_;error[E0130]: `$_` may appear exactly once on the right side of a `|>` --> 06-the-hole-twice-and-outside-a-pipeline.nvs:7:34 |7 | echo $s |> Core\Str::replace($_, $_, "x"), "\n"; | ^^ the hole was already taken by an earlier `$_` = help: bind the value to a local instead
error[E0131]: `$_` is the pipeline hole and has no meaning here --> 06-the-hole-twice-and-outside-a-pipeline.nvs:8:13 |8 | string $t = $_; | ^^ outside the right side of a `|>` = help: a hole is only written on the right side of `|>`
error: aborting due to 2 errorsTo read what a code means and how to fix it, run `nvs agent show <code>`.Details
The compiler replaces $_ with the value on the left. So a pipeline has the same type, the same errors and the same result as the nested calls it replaces. A tainted string stays tainted, and a ?string value on the left is the error E0401 at a step that needs a string. The value on the left runs once.
A step is a call, an array index, a property, a method call or an expression in brackets. $xs |> $_[1], $p |> $_->name, 4 |> $p->add($_), 2 |> self::f($_) and 3 |> new Point($_) all work. For any other expression, write brackets around it: $n |> ($_ * 2).
The step ends where the call, index or brackets end. In $n |> $_ * 2, the step is only $_, and then the result is multiplied by 2. The result is the same here, but $n |> 10 - $_ does not compile: the step is 10, which has no $_.
$_ may be deep inside the step. $s |> Core\Str::lower(Core\Str::trim($_)) works. So does a second pipeline in an argument, or an anonymous function in an argument that uses $_.
These are the three errors:
- A step without
$_is the errorE0129. A method reference such asCore\Str::upperand an anonymous function are steps without$_too. - A step with
$_twice is the errorE0130. Store the value in a variable first. $_anywhere else is the errorE0131. This includes$_as a parameter name, and the second$_in$n |> $_ * $_.
|> runs after a prefix - and before every operator with two sides. "a" . "b" |> Core\Str::upper($_) prints aB, and $s ?? "d" |> Core\Str::upper($_) is $s ?? Core\Str::upper("d"). ! runs after |>, so !$a |> f($_) is !f($a).
Use this when you apply several steps to one value
Section titled “Use this when you apply several steps to one value”A chain of |> reads in the order the steps happen. Nested calls read from the inside out.
Do and don’t
Section titled “Do and don’t”- Do use
|>for three or more steps on one value. - Do put one step on each line when the chain is long.
- Don’t use
|>for a single call.Core\Str::trim($s)is shorter. - Don’t pass a function name such as
Core\Str::upperon the right side. Write the call with$_.
