Skip to content

Commit 544bd80

Browse files
committed
First version of the contexts document
This would close #732. Problem is it is part of a larger document, #1225, which is by no means complete. Also was recently added to #114 as part of the language docs.
1 parent 48a2d0e commit 544bd80

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

doc/Language/contexts.pod6

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=begin pod :tag<perl6>
2+
3+
=TITLE Contexts and contextualizers
4+
5+
=SUBTITLE What are contexts and how to get into them.
6+
7+
A context is needed, in many occasions, to interpret the value of a container. In Perl 6, we will use context to coerce the value of a container into some type or class, or decide what to do with it, as in the case of tne sink context.
8+
9+
=head1 Sink
10+
11+
I<Sink> is equivalent to C<void> context, that is, a context in which we throw (down the sink, as it were) the result of an operation or the return value from a block. In general, this context will be invoked in warnings and errors when a statement does not know what to do with that value.
12+
13+
=begin code
14+
my $sub = -> $a { return $a² };
15+
$sub; # OUTPUT: «WARNINGS:␤Useless use of $sub in sink context (line 1)␤»
16+
=end code
17+
18+
You can force that sink context on L<Iterator>s, by using the L<C<sink-all>|/routine/sink-all> method. L<Proc>s can also be L<sunk via the C<sink> method|https://docs.perl6.org/type/Proc#method_sink>, forcing them to raise an exception and not returning anything.
19+
20+
In general, blocks will warn if evaluated in sink context; however, L<gather/take blocks|/language/control#Flow%29_gather_take> are explicitly evaluated in sink context, with values returned explicitly using C<take>.
21+
22+
=end pod
23+
24+
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6

0 commit comments

Comments
 (0)