class Whatever
Placeholder for the value of an unspecified argument
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-currying. 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 Callables
are accepted.
my = * + 2; # same as -> $x { $x + 2 };say (4); # OUTPUT: «6»
Multiple *
in one expression generate closures with as many arguments:
my = * + *; # same as -> $x, $y { $x + $y }
Using *
in complex expressions will also generate closures:
my = 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 curry *
(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.new(:from(1), :to(*)); |
series operator | 1 ... * | infinite list |
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 curry with Whatever
-stars, but they do curry 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 = 1..4;say [0..*]; # OUTPUT: «(1 2 3 4)»say [0..*-2]; # OUTPUT: «(1 2 3)»
Because Whatever-currying is a purely syntactic compiler transform, you will get no runtime currying of stored Whatever
-stars into WhateverCode
s.
my = *;+ 2; # Not a closure, dies because it can't coerce $x to NumericCATCH ;# 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 curry-exception cases mentioned above. For example, if you want an infinite series by default.
my = potential-upper-limit() // *;my = known-lower-limit() ... ;
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 curried, but rather the *
on the left-hand side.
my = find-constraint() // *;my = * ~~ ;
If this hypothetical find-constraint
were to have found no constraint, $maybe-always-matcher
would evaluate to True
for anything.
(555); # True(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: Mu )multi method ACCEPTS(Whatever: Mu )
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
Defined as:
method Capture()
Throws X::Cannot::Capture
.