method ACCEPTS
Documentation for method ACCEPTS
assembled from the following types:
class Numeric
From Numeric
(Numeric) method ACCEPTS
multi method ACCEPTS(Numeric: )
Returns True
if $other
can be coerced to Numeric and is numerically equal to the invocant (or both evaluate to NaN
).
class RatStr
From RatStr
(RatStr) method ACCEPTS
Defined as:
multi method ACCEPTS(RatStr: Any )
If $value
is Numeric (including another allomorph), checks if invocant's Numeric part ACCEPTS the $value
. If $value
is Str, checks if invocant's Str part ACCEPTS the $value
. If value is anything else, checks if both Numeric and Str parts ACCEPTS
the $value
.
say <5.0> ~~ "5"; # OUTPUT: «False»say <5.0> ~~ 5 ; # OUTPUT: «True»say <5.0> ~~ <5>; # OUTPUT: «True»
class Code
From Code
(Code) method ACCEPTS
multi method ACCEPTS(Code: Mu )
Usually calls the code object and passes $topic
as an argument. However, when called on a code object that takes no arguments, the code object is invoked with no arguments and $topic
is dropped. The result of the call is returned.
role Baggy
From Baggy
(Baggy) method ACCEPTS
Defined as:
method ACCEPTS( --> Bool)
Used in smartmatching if the right-hand side is a Baggy
.
If the right-hand side is the type object, i.e. Baggy
, the method returns True
if $other
does Baggy
otherwise False
is returned.
If the right-hand side is a Baggy
object, True
is returned only if $other
has the same elements, with the same weights, as the invocant.
my = bag <eggs bacon>;say ~~ Baggy; # OUTPUT: «True»say .does(Baggy); # OUTPUT: «True»my = (eggs => 1, bacon => 1).Mix;say ~~ ; # OUTPUT: «True»my = (eggs => 1, bacon => 2).Bag;say ~~ ; # OUTPUT: «False»
class Mu
From Mu
(Mu) method ACCEPTS
multi method ACCEPTS(Mu: )
ACCEPTS
is the method that smartmatching with the infix ~~ operator and given/when invokes on the right-hand side (the matcher).
The Mu:U
multi performs a type check. Returns True
if $other
conforms to the invocant (which is always a type object or failure).
say 42 ~~ Mu; # OUTPUT: «True»say 42 ~~ Int; # OUTPUT: «True»say 42 ~~ Str; # OUTPUT: «False»
Note that there is no multi for defined invocants; this is to allow autothreading of junctions, which happens as a fallback mechanism when no direct candidate is available to dispatch to.
role Setty
From Setty
(Setty) method ACCEPTS
method ACCEPTS()
Returns True
if $other
and self
contain all the same elements, and no others.
class ComplexStr
From ComplexStr
(ComplexStr) method ACCEPTS
Defined as:
multi method ACCEPTS(ComplexStr: Any )
If $value
is Numeric (including another allomorph), checks if invocant's Numeric part ACCEPTS the $value
. If $value
is Str, checks if invocant's Str part ACCEPTS the $value
. If value is anything else, checks if both Numeric and Str parts ACCEPTS
the $value
.
say < 5+0i> ~~ "5.0"; # OUTPUT: «False»say < 5+0i> ~~ 5.0 ; # OUTPUT: «True»say < 5+0i> ~~ <5.0>; # OUTPUT: «True»
class IntStr
From IntStr
(IntStr) method ACCEPTS
Defined as:
multi method ACCEPTS(IntStr: Any )
If $value
is Numeric (including another allomorph), checks if invocant's Numeric part ACCEPTS the $value
. If $value
is Str, checks if invocant's Str part ACCEPTS the $value
. If value is anything else, checks if both Numeric and Str parts ACCEPTS
the $value
.
say <5> ~~ "5.0"; # OUTPUT: «False»say <5> ~~ 5.0 ; # OUTPUT: «True»say <5> ~~ <5.0>; # OUTPUT: «True»
class Range
From Range
(Range) method ACCEPTS
Defined as
multi method ACCEPTS(Range: Mu \topic)multi method ACCEPTS(Range: Range \topic)multi method ACCEPTS(Range: Cool \got)multi method ACCEPTS(Range: Complex \got)
Indicates if the Range
contains (overlaps with) another Range
. As an example:
my = Range.new( 3, 5 );my = Range.new( 1, 10 );say .ACCEPTS( ); # OUTPUT: «False»say .ACCEPTS( ); # OUTPUT: «True»say ~~ ; # OUTPUT: «False» (same as $p.ACCEPTS( $r )say ~~ ; # OUTPUT: «True» (same as $r.ACCEPTS( $p )
Of course, an infinite Range
always contains another Range
, therefore:
say 1..10 ~~ -∞..∞; # OUTPUT: «True»say 1..10 ~~ -∞^..^∞; # OUTPUT: «True»
Similarly, a Range
with open boundaries often includes other ranges:
say 1..2 ~~ *..10; # OUTPUT: «True»say 2..5 ~~ 1..*; # OUTPUT: «True»
It is also possible to use non-numeric ranges, for instance string based ones:
say 'a'..'j' ~~ 'b'..'c'; # OUTPUT: «False»say 'b'..'c' ~~ 'a'..'j'; # OUTPUT: «True»say 'perl' ~~ -∞^..^∞; # OUTPUT: «True»say 'perl' ~~ -∞..∞; # OUTPUT: «True»say 'perl' ~~ 1..*; # OUTPUT: «True»
When smartmatching a Range
of integers with a Cool (string) the ACCEPTS
methods exploits the before and after operators in order to check that the Cool
value is overlapping the range:
say 1.10 ~~ '5'; # OUTPUT: «False»say '5' before 1; # OUTPUT: «False»say '5' after 10; # OUTPUT: «True»say '5' ~~ *..10; # OUTPUT: «False»
In the above example, since the '5'
string is after the 10
integer value, the Range
does not overlap with the specified value.
When matching with a Mu
instance (i.e., a generic instance), the cmp operator is used.
enum Bool
From Bool
(Bool) method ACCEPTS
method ACCEPTS(Bool: --> Bool)
Used for smartmatch comparison. When the right side is True
returns always True
, when the right side of the match is False
returns always False
. In particular, ACCEPTS
returns always the instance on which it is invoked, that is the right side of a smartmatch. As an example:
my = Bool.new( True );# when True on the right side returns# always TrueTrue ~~ ; # TrueFalse ~~ ; # True= Bool.new( False );# when False on the right side# returns always FalseFalse ~~ ; # FalseTrue ~~ ; # False
class Any
From Any
(Any) method ACCEPTS
Defined as:
multi method ACCEPTS(Any: Mu )
Usage:
EXPR.ACCEPTS(EXPR);
Returns True
if $other === self
(i.e. it checks object identity).
Many built-in types override this for more specific comparisons.
class Map
From Map
(Map) method ACCEPTS
Defined as:
multi method ACCEPTS(Map: Positional )multi method ACCEPTS(Map: Cool )multi method ACCEPTS(Map: Regex )multi method ACCEPTS(Map: Any )
Used in smartmatching if the right-hand side is an Map
.
If the topic is list-like (Positional), returns True if any of the list elements exist as a key in the Map.
If the topic is of type Cool
(strings, integers etc.), returns True if the topic exists as a key.
If the topic is a regex, returns True if any of the keys match the regex.
As a fallback, the topic is coerced to a list, and the Positional
behavior is applied.
class Pair
From Pair
(Pair) method ACCEPTS
Defined as:
multi method ACCEPTS(Pair $: )multi method ACCEPTS(Pair $: Pair )multi method ACCEPTS(Pair $: Mu )
If %topic
is an Associative, looks up the value using invocant's key in it and checks invocant's value .ACCEPTS
that value:
say %(:42a) ~~ :42a; # OUTPUT: «True»say %(:42a) ~~ :10a; # OUTPUT: «False»
If $topic
is another Pair, checks the invocant's value .ACCEPTS
the $topic
's value. Note that the keys are not considered and can be different:
say :42a ~~ :42a; # OUTPUT: «True»say :42z ~~ :42a; # OUTPUT: «True»say :10z ~~ :42a; # OUTPUT: «False»
If $topic
is any other value, the invocant Pair
's key is treated as a method name. This method is called on $topic
, the boolean result of which is compared against the invocant Pair
's boolean value. For example, primality can be tested using smartmatch:
say 3 ~~ :is-prime; # OUTPUT: «True»say 3 ~~ is-prime => 'truthy'; # OUTPUT: «True»say 4 ~~ :is-prime; # OUTPUT: «False»
This form can also be used to check Bool values of multiple methods on the same object, such as IO::Path, by using Junctions:
say "foo" .IO ~~ :f & :rw; # OUTPUT: «False»say "/tmp".IO ~~ :!f; # OUTPUT: «True»say "." .IO ~~ :f | :d; # OUTPUT: «True»
class Str
From Str
(Str) method ACCEPTS
multi method ACCEPTS(Str: )
Returns True
if the string is the same as $other
.
class Signature
From Signature
(Signature) method ACCEPTS
multi method ACCEPTS(Signature: Signature )multi method ACCEPTS(Signature: Capture )multi method ACCEPTS(Signature: Mu \topic)
If $topic
is a Signature returns True
if anything accepted by $topic
would also be accepted by the invocant, otherwise returns False
:
:(, ) ~~ :(, , ?); # OUTPUT: «True»:(Int ) ~~ :(Str); # OUTPUT: «False»
The $topic
is a Capture, returns True
if it can be bound to the invocant, i.e., if a function with invocant's Signature
would be able to be called with the $topic
:
\(1, 2, :foo) ~~ :(, , :foo()); # OUTPUT: «True»\(1, :bar) ~~ :(); # OUTPUT: «False»
Lastly, the candidate with Mu \topic
converts topic
to Capture and follows the same semantics as Capture $topic
:
<a b c d> ~~ :(Int ); # OUTPUT: «False»42 ~~ :(Int); # OUTPUT: «False» (Int.Capture throws)set(<a b>) ~~ :(:, :); # OUTPUT: «True»
Since where
clauses are not introspectable, the method cannot determine whether two signatures ACCEPTS the same sort of where
-constrained parameters. Such comparisons will return False
. This includes signatures with literals, which are just sugar for the where
-constraints:
say :(42) ~~ :($ where 42) # OUTPUT: «False»
class Whatever
From Whatever
(Whatever) 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»
class NumStr
From NumStr
(NumStr) method ACCEPTS
Defined as:
multi method ACCEPTS(NumStr: Any )
If $value
is Numeric (including another allomorph), checks if invocant's Numeric part ACCEPTS the $value
. If $value
is Str, checks if invocant's Str part ACCEPTS the $value
. If value is anything else, checks if both Numeric and Str parts ACCEPTS
the $value
.
say <5e0> ~~ "5.0"; # OUTPUT: «False»say <5e0> ~~ 5.0 ; # OUTPUT: «True»say <5e0> ~~ <5.0>; # OUTPUT: «True»
class Regex
From Regex
(Regex) method ACCEPTS
multi method ACCEPTS(Regex: Mu --> Match)multi method ACCEPTS(Regex: @)multi method ACCEPTS(Regex: %)
Matches the regex against the argument passed in. If the argument is Positional, it returns the first successful match of any list item. If the argument is Associative, it returns the first successful match of any key. Otherwise it interprets the argument as a Str and matches against it.
In the case of Positional and Associative matches, Nil is returned on failure.
class List
From List
(List) method ACCEPTS
Defined as:
multi method ACCEPTS(List: )
If $topic
is an Iterable, returns True
or False
based on whether the contents of the two Iterables
match. A Whatever element in the invocant matches anything in the corresponding position of the $topic
Iterable
. A HyperWhatever matches any number of any elements, including no elements:
say (1, 2, 3) ~~ (1, *, 3); # OUTPUT: «True»say (1, 2, 3) ~~ (9, *, 5); # OUTPUT: «False»say (1, 2, 3) ~~ ( **, 3); # OUTPUT: «True»say (1, 2, 3) ~~ ( **, 5); # OUTPUT: «False»say (1, 3) ~~ (1, **, 3); # OUTPUT: «True»say (1, 2, 4, 5, 3) ~~ (1, **, 3); # OUTPUT: «True»say (1, 2, 4, 5, 6) ~~ (1, **, 5); # OUTPUT: «False»say (1, 2, 4, 5, 6) ~~ ( ** ); # OUTPUT: «True»say () ~~ ( ** ); # OUTPUT: «True»
In addition, returns False
if either the invocant or $topic
is a lazy Iterable
, unless $topic
is the same object as the invocant, in which case True
is returned.
If $topic
is not an Iterable, returns the invocant if the invocant has no elements or its first element is a Match object (this behavior powers m:g//
smartmatch), or False
otherwise.
class IO::Path
From IO::Path
(IO::Path) method ACCEPTS
Defined as:
multi method ACCEPTS(IO::Path: Cool --> Bool)
Coerces the argument to IO::Path
, if necessary. Returns True
if .absolute
method on both paths returns the same string. NOTE: it's possible for two paths that superficially point to the same resource to NOT smartmatch as True
, if they were constructed differently and were never fully resolved:
say "foo/../bar".IO ~~ "bar".IO # False
The reason is the two paths above may point to different resources when fully resolved (e.g. if foo
is a symlink). Resolve the paths before smartmatching to check they point to same resource:
say "foo/../bar".IO.resolve(:completely) ~~ "bar".IO.resolve(:completely) # True