Skip to content

Commit

Permalink
First version of the contexts document
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JJ committed May 16, 2018
1 parent 48a2d0e commit 544bd80
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions doc/Language/contexts.pod6
@@ -0,0 +1,24 @@
=begin pod :tag<perl6>
=TITLE Contexts and contextualizers
=SUBTITLE What are contexts and how to get into them.
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.
=head1 Sink
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.
=begin code
my $sub = -> $a { return $a² };
$sub; # OUTPUT: «WARNINGS:␤Useless use of $sub in sink context (line 1)␤»
=end code
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.
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>.
=end pod

# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6

0 comments on commit 544bd80

Please sign in to comment.