class Whatever { }
Whatever
is a class whose objects don't have any explicit meaning; it gets its semantics from other routines that accept Whatever
-objects as markers to do something special. Using the *
literal as an operand creates a Whatever
object.
Much of *
's charm comes from Whatever-priming. When *
is used in term position, that is, as an operand, in combination with most operators, the compiler will transform the expression into a closure of type WhateverCode
, which is actually a Block
that can be used wherever Callable
s are accepted.
my $c = * + 2; # same as -> $x { $x + 2 }; say $c(4); # OUTPUT: «6»
Multiple *
in one expression generate closures with as many arguments:
my $c = * + *; # same as -> $x, $y { $x + $y }
Using *
in complex expressions will also generate closures:
my $c = 4 * * + 5; # same as -> $x { 4 * $x + 5 }
Calling a method on *
also creates a closure:
<a b c>.map: *.uc; # same as <a b c>.map: -> $char { $char.uc }
As mentioned before, not all operators and syntactic constructs prime *
(or Whatever
-stars) to WhateverCode
. In the following cases, *
will remain a Whatever
object.
Exception | Example | What it does |
---|---|---|
comma | 1, *, 2 | generates a List with a * element |
range operators | 1 .. * | Range from 1 to Inf |
series operator | 1 ... * | infinite lazy Seq |
assignment | $x = * | assign * to $x |
binding | $x := * | bind * to $x |
list repetition | 1 xx * | generates an infinite list |
The range operators are handled specially. They do not prime with Whatever
-stars, but they do prime with WhateverCode
say (1..*).^name; # OUTPUT: «Range» say ((1..*-1)).^name; # OUTPUT: «WhateverCode»
This allows all these constructs to work:
.say for 1..*; # infinite loop
and
my @a = 1..4; say @a[0..*]; # OUTPUT: «(1 2 3 4)» say @a[0..*-2]; # OUTPUT: «(1 2 3)»
Because Whatever-priming is a purely syntactic compiler transform, you will get no runtime priming of stored Whatever
-stars into WhateverCode
s.
my $x = *; $x + 2; # Not a closure, dies because it can't coerce $x to Numeric CATCH { default { put .^name, ': ', .Str } }; # OUTPUT: «X::Multi::NoMatch: Cannot resolve caller Numeric(Whatever: ); # none of these signatures match: # (Mu:U \v: *%_)»
The use cases for stored Whatever
-stars involve those prime-exception cases mentioned above. For example, if you want an infinite series by default.
my $max = potential-upper-limit() // *; my $series = known-lower-limit() ... $max;
A stored *
will also result in the generation of a WhateverCode
in the specific case of smartmatch. Note that this is not actually the stored *
which is being primed, but rather the *
on the left-hand side.
my $constraint = find-constraint() // *; my $maybe-always-matcher = * ~~ $constraint;
If this hypothetical find-constraint
were to have found no constraint, $maybe-always-matcher
would evaluate to True
for anything.
$maybe-always-matcher(555); # True $maybe-always-matcher(Any); # True
HyperWhatever
's functionality is similar to Whatever
, except it refers to multiple values, instead of a single one.
Methods§
method ACCEPTS§
multi method ACCEPTS(Whatever:D: Mu $other) multi method ACCEPTS(Whatever:U: Mu $other)
If the invocant is an instance, always returns True
. If the invocant is a type object, performs a typecheck.
say 42 ~~ (*); # OUTPUT: «True» say 42 ~~ Whatever; # OUTPUT: «False»
method Capture§
method Capture()
Throws X::Cannot::Capture
.