Skip to content

Commit c6dc297

Browse files
ab5tractjonathanstowe
authored andcommitted
Add a few clarifications, re-wordings, and expansions to the Whatever docs
1 parent 575666c commit c6dc297

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

lib/Type/Whatever.pod

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ its semantic from other routines that accept C<Whatever>-objects as markers
1111
to do something special. The C<*> literal in term position creates a
1212
C<Whatever> object.
1313
14-
Another source of speciality is that the compiler turns
15-
combinations of C<*> in term position and many operators into closures.
16-
This process is called I<Whatever-currying>.
14+
Much of C<*>'s charm comes from I<Whatever-currying>. When C<*> is used in term
15+
position in combination with most operators, the compiler will transform the
16+
expression into a closure of type L<WhateverCode>.
1717
1818
my $c = * + 2; # same as -> $x { $x + 2 };
1919
say $c(4); # 6
@@ -22,17 +22,17 @@ Multiple C<*> in one expression generate closures with as many arguments:
2222
2323
my $c = * + *; # same as -> $x, $y { $x + $y }
2424
25-
C<*> in complex expressions also generate closures:
25+
Using C<*> in complex expressions will also generate closures:
2626
2727
my $c = 4 * * + 5; # same as -> $x { 4 * $x + 5 }
2828
29-
Calling a method on C<*> also create a closure:
29+
Calling a method on C<*> also creates a closure:
3030
31-
say <a b c>.map: *.uc; # A B C
31+
<a b c>.map: *.uc; # same as <a b c>.map: -> $char { $char.uc }
3232
33-
Those closure are of type L<WhateverCode>.
34-
35-
Not all operators and syntactic constructs curry Whatever-stars.
33+
As mentioned before, not all operators and syntactic constructs curry C<*> (or
34+
C<Whatever>-stars) to C<WhateverCode>. In the following cases, C<*> will remain
35+
a C<Whatever> object.
3636
3737
=begin table
3838
@@ -58,15 +58,34 @@ This allow all these constructs to work:
5858
5959
.say for 1..*; # infinite loop
6060
my @a = 1..4;
61-
say @a[1..*]; # 2 3 4
62-
say @a[1..*-2]; # 2 3
61+
say @a[0..*]; # 1 2 3 4
62+
say @a[0..*-2]; # 1 2 3
6363
64-
The currying is purely syntactic.
64+
Because I<Whatever-currying> is a purely syntactic compiler transform, you will get no
65+
runtime currying of stored C<Whatever>-stars into C<WhateverCode>s.
6566
6667
my $x = *;
6768
$x + 2; # not a closure, dies because
6869
# it can't coerce $x to Numeric
6970
71+
The use cases for stored C<Whatever>-stars are involve those curry-exception
72+
cases mentioned above. For example, if you want an infinite series by default.
73+
74+
my $max = potential-upper-limit() // *;
75+
my $series = known-lower-limit() ... $max;
76+
77+
A stored C<*> will also result in the generation of a C<WhateverCode> in the specific
78+
case of smart match.
79+
80+
my $constraint = find-constraint() // *;
81+
my $maybe-always-matcher = * ~~ $constraint;
82+
83+
If this hypothetical C<find-constraint> were to have found no constraint, C<$maybe-always-matcher>
84+
would return to C<True> for anything.
85+
86+
$maybe-always-matcher(555); # True
87+
$maybe-always-matcher(Any); # True
88+
7089
=head1 Methods
7190
7291
=head2 method ACCEPTS

0 commit comments

Comments
 (0)