Skip to content

Instantly share code, notes, and snippets.

@dugword
Created December 29, 2016 22:01
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 dugword/d4e5b0dfbf02484df5802d49d4f59ba4 to your computer and use it in GitHub Desktop.
Save dugword/d4e5b0dfbf02484df5802d49d4f59ba4 to your computer and use it in GitHub Desktop.
multi method new in Perl 6 Class Raw
#!/usr/bin/env perl6
say "Testing classes";
my class Foo {
has $.bar;
has $.baz;
multi method new (:$bar!, :$baz!) {
return self.bless(:$bar, :$baz);
}
multi method new (:$baz!) {
my $bar = 'bar';
# my $baz = $baz
return self.bless(:$bar, :$baz);
}
multi method new (:$bar!) {
my $baz = 'baz';
return self.bless(:$bar, :$baz);
}
multi method new {
my $bar = 'foo';
my $baz = 'foo';
return self.bless(:$bar, :$baz);
}
}
my $foo1 = Foo.new();
my $foo2 = Foo.new(bar => 'puppies');
my $foo3 = Foo.new(baz => 'kitties');
my $foo4 = Foo.new(baz => 'ponies', bar => 'butterflies');
dd $foo1;
dd $foo2;
dd $foo3;
dd $foo4;
## OUTPUT
# Testing classes
# Foo $foo1 = Foo.new(bar => "foo", baz => "foo")
# Foo $foo2 = Foo.new(bar => "puppies", baz => "baz")
# Foo $foo3 = Foo.new(bar => "bar", baz => "kitties")
# Foo $foo4 = Foo.new(bar => "butterflies", baz => "ponies")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment