Skip to content

Instantly share code, notes, and snippets.

@treyharris
Last active April 11, 2017 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save treyharris/919902805f27f447893e43b504d246bd to your computer and use it in GitHub Desktop.
Save treyharris/919902805f27f447893e43b504d246bd to your computer and use it in GitHub Desktop.
use v6;
# I want the following behavior:
# >> Foo.hrm(3)
# "hrm, 3"
# >> my $f = Foo.new
# >> $f.hrm(3)
# "hrm, 3"
# >> $f.minimum = 3
# >> $f.hrm(3)
# Bigger!
#
# This way works, but isn't DRY:
class Foo {
has Int $.minimum is rw = 0;
# Can I avoid naming "Foo" in the signature here somehow?
# (:D $:) isn't valid.
multi method hrm(::?CLASS:U $: Int $n) {
self.new.hrm($n);
# The above works. But is there Any way to avoid repeating "hrm"
# here?
#
# nextwith(self.new(): $n)
# Gives: "No such method 'nextwith' for invocant of type 'Foo'"
#
# nextwith(self.new(); $n)
# Seems to work at first:
# % perl6 -M Foo -e 'Foo.hrm("blah")'
# Cannot resolve caller AUTOGEN(Foo: Str); none of these signatures match:
# (Foo:U $: Int $n, *%_)
# (Foo:D $: Int $n, *%_)
# in block <unit> at -e line 1
# But doesn't actually do the dispatch:
# >> Foo.hrm(3)
# Nil
# >> Foo.hrm(-10)
# Nil
#
# My attempts to create a Capture first and then use it to call
# hrm were also unsuccessful, but that could be just my not
# knowing the right syntax.
}
# Same issue with naming Foo here...
multi method hrm(::?CLASS:D $: Int $n) {
if $n < $.minimum {
say "Bigger!";
} else {
say "hrm, $n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment