Skip to content

Commit cae275b

Browse files
committed
Link up (hopefully, contingent on markup working) wantarray sections
Also, provide examples for alternatives to wantarray
1 parent 2e7ab45 commit cae275b

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

doc/Language/5to6-perlfunc.pod

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,74 @@ As with C<wait>, the disposition of this is unclear.
17271727
17281728
=item wantarray
17291729
1730-
There is no C<wantarray> in Perl 6.
1730+
There is no C<wantarray> in Perl 6, because L<reasons|/language/faq#Why_is_wantarray_or_want_gone?_Can_I_return_different_things_in_different_contexts?>.
1731+
1732+
There are very easy ways to cover many of the use cases which wantarray filled.
1733+
1734+
First, since Perl 6 does not need special reference syntax to contain
1735+
a C<List> or C<Array> in a C<Scalar>, simply returning a list may be
1736+
all that is needed:
1737+
1738+
sub listofstuff {
1739+
return 1,2,3;
1740+
}
1741+
my $a = listofstuff();
1742+
print $a; # prints "123"
1743+
print join("<",listofstuff()) # prints "1<2<3"
1744+
1745+
One of the most common use cases is to provide either an array of lines
1746+
or elements, or a prettier string than would be produced by simply
1747+
printing the array. One can mix in a custom C<.Str> method for this
1748+
purpose:
1749+
1750+
sub prettylist(*@origlist) {
1751+
@origlist but role {
1752+
method Str { self.join("<") }
1753+
}
1754+
}
1755+
print prettylist(1,2,3); # prints "1<2<3"
1756+
print join(">", prettylist(3,2,1)); # prints "3>2>1"
1757+
1758+
In the above example, the returned list may be lazy, and the C<.Str> method
1759+
is not called until stringification happens, so no extra work is done
1760+
to generate something which is not asked for.
1761+
1762+
Another use case is to create methods which are mutators when called
1763+
in void context but produce copies during assignment. It is generally
1764+
considered better form in Perl 6 not to do so, since users can quite
1765+
easily turn any copy-producing method into a mutator using the C<.=>
1766+
operator:
1767+
1768+
my $a = "foo\n";
1769+
$a.ords.say; # says "(102 111 111 10)"
1770+
$a .= chomp;
1771+
$a.ords.say; # says "(102 111 111)"
1772+
1773+
However if you have your heart set on using the same function
1774+
name for both operations, you can get most of the way there by mixing in
1775+
a C<.sink> method, which will be called when the result finds itself
1776+
in void context. There are some caveats however, so again, this is
1777+
not advised:
1778+
1779+
multi sub increment($b is rw) {
1780+
($b + 1) does role { method sink { $b++ } }
1781+
}
1782+
multi sub increment($b) {
1783+
$b + 1
1784+
}
1785+
my $a = 1;
1786+
increment($a);
1787+
say $a; # says "2"
1788+
my $b = increment($a);
1789+
say $a, $b; # says "2 3"
1790+
# ...users will just have to be aware that they should not accidentally
1791+
# sink a stored value later, though this requires some effort to
1792+
# actually do:
1793+
sub identity($c is rw) { $c };
1794+
$a = 1;
1795+
$b = increment($a);
1796+
identity($b);
1797+
$a.say; # says "2"
17311798
17321799
=head2 warn
17331800

doc/Language/faq.pod

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,11 @@ the exception with the C<exception> method.
457457
458458
=head2 Why is C<wantarray> or C<want> gone? Can I return different things in different contexts?
459459
460-
Perl 5 has the C<wantarray> function that tells you whether it is called in
461-
void, scalar or list context. Perl 6 has no equivalent construct,
462-
because context does not flow inwards, i.e. a routine would need time travel to know which
463-
context it is called in because context is lazy, known only when the results are used later.
460+
Perl 5 has the L<C<wantarray>|/language/5to6-perlfunc#wantarray> function that
461+
tells you whether it is called in void, scalar or list context. Perl 6 has no
462+
equivalent construct, because context does not flow inwards, i.e. a routine would need
463+
time travel to know which context it is called in because context is lazy, known
464+
only when the results are used later.
464465
465466
For example, Perl 6 has multiple dispatch, so in a code example like
466467
@@ -473,8 +474,12 @@ an integer, because it is not yet known what the caller is. In general
473474
this requires solving the halting problem, which even Perl 6 compiler
474475
writers have trouble with.
475476
476-
The way to achieve context sensitivity in Perl 6 is to return an object
477+
One way to achieve context sensitivity in Perl 6 is to return an object
477478
that knows how to respond to method calls that are typical for a context.
479+
In Perl 6 this is actually a L<lot easier|/language/5to6-perlfunc#wantarray>
480+
than it may sound, and other features of the language either mitigate the
481+
need to do so in the first place, or make it possible to cover most of the
482+
use cases of wantarray.
478483
479484
For example regex matches return L<Match objects that know how to respond
480485
to list indexing, hash indexing, and that can turn into the matched

0 commit comments

Comments
 (0)