I'm going to try go get you to think about adverbs the way I do.
Imagine an object with a move
method
class Foo
method move ( $direction ) {…}
}
To get it to move to the right you might write
Foo.new.move('right');
Now what if that doesn't move fast enough for you, you may want to write something like this
Foo.new.move('right', :quickly);
So a object is a noun, a method is a verb, and an adverb is an adverb.
Now it would be nice to be able to re-use that syntax for modifying more things like:
- quoting
q :backslash '\n'
- hash access
%h{'b'}:delete
- other user-defined operators
'a' (foobar) 'b' :normalize
That's useful enough, but what if you want to match the second thing a regex would match.
'Foo Bar' ~~ m :nd(2) / <:Lu> <:Ll>+ /; # 「Bar」
How about allowing :2nd
be an alias to :nd(2)
'Foo Bar' ~~ m :2nd / <:Lu> <:Ll>+ /; # 「Bar」
And have :quickly
just be short for :quickly(True)
.
How about we add :!quickly
and have it short for :quickly(False)
.
That gives us really nice syntax, but how does it actually work?
Well there is the idea of named parameters quickly => True
from Perl 5, so how about it just be generalization for that.
class Foo
# there are more ways to handle named parameters than this
multi method move ( $direction ) {…}
multi method move ( $direction, :$quickly! ) {…}
}
Foo.new.move( 'right', :quickly )
Foo.new.move( 'right' ) :quickly
Foo.new.move( 'right', :quickly(True) )
Foo.new.move( 'right', ) :quickly(True)
Foo.new.move( 'right', quickly => True )
I've only touched on the subject, but it doesn't really get any harder than this.
It's widespread reuse of features like this that are why we often call Perl 6 strangely consistent.