What is precedence dropping call syntax?
Perl 6 introduces a lot of syntax changes from its predecessor (or cousin?), and one of it is the so called precedence dropping in method call. The syntax follows the following rules:
- no need for brackets;
- a comma
:
before the argument list; - all things after the comma are managed as a list.
class Foo {
method foo( *@v ) {
say @v;
return 1;
}
}
my $f = Foo.new;
say $f.foo( 'a', 'b', 'c').^name;
[a b c]
Int
foo
method and then chained the name
on its return value, that is an integer.
Now let’s rewrite this with precedence dropping:
$f.foo: 'a', 'b', 'c'.^name;
[a b Str]
name
is not applied to the method return value but to the last argument.
In other words precedence dropping means that what is following the method call has the precedence on the call itself, and therefore is evaluated before it is passed to the method itself.
So the following are equivalent:
$f.foo: 'a', 'b', 'c'.^name;
$f.foo( 'a', 'b', 'c'.^name );
$f.foo: ( 'a', 'b', 'c' ).^name; # [List]
name
before and then the method is called.
So when is precedence dropping useful? Well, in my opinion when you are hitting the last method call of a chain call you can quite safely apply precedence dropping, and this avoid parenthesis balance problems:
$f.baz.bar.map.sort.reverse.foo: 'a','b','c';
# ^^^ end of method chain!
$f.bar.baz.sort.foo: 'a', 'b', 'c' .reverse # wrong!