Documentation for method ACCEPTS

Documentation for method ACCEPTS, assembled from the following types:

class Mu

From Mu

(Mu) method ACCEPTS

multi method ACCEPTS(Mu:U: $other)

ACCEPTS is the method that smart matching 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 Any

From Any

(Any) method ACCEPTS

Defined as:

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



Returns True if $other === self (i.e. it checks object identity).

Many built-in types override this for more specific comparisons

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 behaviour powers m:g// smartmatch), or False otherwise.

role Baggy

From Baggy

(Baggy) method ACCEPTS

Defined as:

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

Used in smart-matching 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␤» 

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.

role Numeric

From Numeric

(Numeric) method ACCEPTS

multi method ACCEPTS(Numeric:D: $other)

Returns True if $other is numerically the same as the invocant.

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 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 smart-matching 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.

To retrieve a value from the Map by key, use the { } postcircumfix operator:

my $map ='a'1'b'2);
say $map{'a'}# OUTPUT: «1␤» 

To check whether a given key is stored in a Map, modify the access with the :exists adverb:

my $map ='a'1'b'2);
my $key = 'a';
if $map{$key}:exists {
    say "$map{} has key $key";

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 

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, uses the invocant's key as the name of the method to call on topic and check's its .Bool is the same as .Bool of the invocant's value. This form can 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 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.

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 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?);   # RESULT: «True» 
:(Int $n~~ :(Str);                 # RESULT: «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)); # RESULT: «True» 
\(1:bar)    ~~ :($a);                 # RESULT: «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);      # RESULT: «False» 
42         ~~ :(Int);         # RESULT: «False» (Int.Capture throws) 
set(<a b>~~ :(:$a:$b);    # RESULT: «True» 

class Whatever

From Whatever

(Whatever) method ACCEPTS

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

Returns True.