Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |