In Contexts and contextualizers§
See primary documentation in context for Sink
The sink context is equivalent to what other languages call void
context. It is the context which does nothing with the result or return of any code: a term, an operation or a block. In general, when this context consumes a value or variable a warning or error is issued because the value is being ignored. Mnemonics for sink relate to being rid of something: water down a sink's drain; a ship sinking; a heatsink removing warmth.
my $sub = -> $a { $a² }; $sub; # OUTPUT: «WARNINGS:Useless use of $sub in sink context (line 1)»
You can force that sink context on Iterator
s, by using the sink-all
method. Proc
s can also be sunk via the sink
method, forcing them to raise an exception and not return anything.
Most blocks will warn if evaluated in sink context; however, gather/take blocks are explicitly evaluated in sink context, with values returned explicitly using take
:
my @results = gather for 1..1 { ^10 .map: *.take }; say @results; # OUTPUT: «[0 1 2 3 4 5 6 7 8 9]»
In this example, for
is run in sink context, and within it, map
is too. Results are taken explicitly from the loop via gather/take.
In sink context, an object will call its sink
method if present:
sub foo { return [<a b c>] does role { method sink { say "sink called" } } } foo; # OUTPUT: «sink called»