method ACCEPTS

Documentation for method ACCEPTS assembled from the following types:

class List

From List

(List) method ACCEPTS

Defined as:

multi method ACCEPTS(List:D: $topic)

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 (123)       ~~ (1,  *3);  # OUTPUT: «True␤» 
say (123)       ~~ (9,  *5);  # OUTPUT: «False␤» 
say (123)       ~~ (   **3);  # OUTPUT: «True␤» 
say (123)       ~~ (   **5);  # OUTPUT: «False␤» 
say (13)          ~~ (1**3); # OUTPUT: «True␤» 
say (12453~~ (1**3); # OUTPUT: «True␤» 
say (12456~~ (1**5); # OUTPUT: «False␤» 
say (12456~~ (   **   ); # 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 Code

From Code

(Code) method ACCEPTS

multi method ACCEPTS(Code:D: Mu $topic)

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.

class Range

From Range

(Range) method ACCEPTS

Defined as

multi method ACCEPTS(Range:D: Mu \topic)
multi method ACCEPTS(Range:D: Range \topic)
multi method ACCEPTS(Range:D: Cool:D \got)
multi method ACCEPTS(Range:D: Complex:D \got)

Indicates if the Range contains (overlaps with) another Range. As an example:

my $p = Range.new35  );
my $r = Range.new110 );
 
say $p.ACCEPTS$r );    # OUTPUT: «False␤» 
say $r.ACCEPTS$p );    # OUTPUT: «True␤» 
say $r ~~ $p;            # OUTPUT: «False␤»  (same as $p.ACCEPTS( $r ) 
say $p ~~ $r;            # 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.

class Signature

From Signature

(Signature) method ACCEPTS

multi method ACCEPTS(Signature:D: Signature $topic)
multi method ACCEPTS(Signature:D: Capture $topic)
multi method ACCEPTS(Signature:D: Mu \topic)

If $topic is a Signature returns True if anything accepted by $topic would also be accepted by the invocant, otherwise returns False:

:($a$b~~ :($foo$bar$baz?);   # OUTPUT: «True» 
:(Int $n~~ :(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:

\(12:foo~~ :($a$b:foo($bar)); # OUTPUT: «True» 
\(1:bar)    ~~ :($a);                 # 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 $a);      # OUTPUT: «False» 
42         ~~ :(Int);         # OUTPUT: «False» (Int.Capture throws) 
set(<a b>~~ :(:$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 Any

From Any

(Any) method ACCEPTS

Defined as:

multi method ACCEPTS(Any:D: Mu $other)

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.

role Baggy

From Baggy

(Baggy) method ACCEPTS

Defined as:

method ACCEPTS($other --> Bool:D)

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 $breakfast = bag <eggs bacon>;
say $breakfast ~~ Baggy;                            # OUTPUT: «True␤» 
say $breakfast.does(Baggy);                         # OUTPUT: «True␤» 
 
my $second-breakfast = (eggs => 1bacon => 1).Mix;
say $breakfast ~~ $second-breakfast;                # OUTPUT: «True␤» 
 
my $third-breakfast = (eggs => 1bacon => 2).Bag;
say $second-breakfast ~~ $third-breakfast;          # OUTPUT: «False␤»

enum Bool

From Bool

(Bool) method ACCEPTS

method ACCEPTS(Bool:D: --> Bool:D)

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 $b = Bool.newTrue );
# when True on the right side returns 
# always True 
True  ~~ $b;     # True 
False ~~ $b;     # True 
 
$b = Bool.newFalse );
# when False on the right side 
# returns always False 
False ~~ $b;     # False 
True ~~ $b;      # False 

class Whatever

From Whatever

(Whatever) 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␤»

role Setty

From Setty

(Setty) method ACCEPTS

method ACCEPTS($other)

Returns True if $other and self contain all the same elements, and no others.

class Mu

From Mu

(Mu) method ACCEPTS

multi method ACCEPTS(Mu:U: $other)

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.

class Str

From Str

(Str) method ACCEPTS

multi method ACCEPTS(Str:D: $other)

Returns True if the string is the same as $other.

class ComplexStr

From ComplexStr

(ComplexStr) method ACCEPTS

Defined as:

multi method ACCEPTS(ComplexStr:D: Any:D $value)

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 Regex

From Regex

(Regex) method ACCEPTS

multi method ACCEPTS(Regex:D: Mu --> Match:D)
multi method ACCEPTS(Regex:D: @)
multi method ACCEPTS(Regex:D: %)

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 Numeric

From Numeric

(Numeric) method ACCEPTS

multi method ACCEPTS(Numeric:D: $other)

Returns True if $other can be coerced to Numeric and is numerically equal to the invocant (or both evaluate to NaN).

class IntStr

From IntStr

(IntStr) method ACCEPTS

Defined as:

multi method ACCEPTS(IntStr:D: Any:D $value)

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 NumStr

From NumStr

(NumStr) method ACCEPTS

Defined as:

multi method ACCEPTS(NumStr:D: Any:D $value)

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 Pair

From Pair

(Pair) method ACCEPTS

Defined as:

multi method ACCEPTS(Pair:D $: %topic)
multi method ACCEPTS(Pair:D $: Pair:D $topic)
multi method ACCEPTS(Pair:D $: Mu $topic)

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 :42~~ :42a; # OUTPUT: «True␤» 
say :42~~ :42a; # OUTPUT: «True␤» 
say :10~~ :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 RatStr

From RatStr

(RatStr) method ACCEPTS

Defined as:

multi method ACCEPTS(RatStr:D: Any:D $value)

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 Map

From Map

(Map) method ACCEPTS

Defined as:

multi method ACCEPTS(Map:D: Positional $topic)
multi method ACCEPTS(Map:D: Cool:D     $topic)
multi method ACCEPTS(Map:D: Regex      $topic)
multi method ACCEPTS(Map:D: Any        $topic)

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 IO::Path

From IO::Path

(IO::Path) method ACCEPTS

Defined as:

multi method ACCEPTS(IO::Path:D: Cool:D $other --> Bool:D)

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