Documentation for routine ACCEPTS

Documentation for routine 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)

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 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 = Map.new('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 = Map.new('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: Capture $topic)
multi method ACCEPTS(Signature:D: @topic)
multi method ACCEPTS(Signature:D: %topic)
multi method ACCEPTS(Signature:D: Signature $topic)

The first three see if the argument could be bound to the capture, i.e., if a function with that Signature would be able to be called with the $topic:

(12:foo~~ :($a$b:foo($bar));   # RESULT: «True» 
<a b c d> ~~ :(Int $a);                  # RESULT: «False» 

The last returns True if anything accepted by $topic would also be accepted by the Signature.

:($a$b~~ :($foo$bar$baz?);   # RESULT: «True» 
:(Int $n~~ :(Str);                 # RESULT: «False» 

class Whatever

From Whatever

(Whatever) method ACCEPTS

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

Returns True.