Skip to content

Instantly share code, notes, and snippets.

Created October 24, 2015 19:27
Show Gist options
  • Save anonymous/bcd7448642f4d93df39e to your computer and use it in GitHub Desktop.
Save anonymous/bcd7448642f4d93df39e to your computer and use it in GitHub Desktop.
#!env perl6
class Hello { ... }
class T {
has Real $.value is rw;
has Hello $.hello handles 'call_func';
}
class Hello {
has Str $.name;
my %func_map = (
mul => sub (Real $x, Real $y) { return $x * $y },
add => sub (Real $x, Real $y) { return $x + $y },
);
method call_func ($x, $y) {
dd %func_map;
my $t = %func_map{$.name};
$t($x, $y);
}
}
my $x = Hello.new(:levels((3, 3, 3)), :name('add'));
my $t = T.new(:hello($x));
say $t.call_func(3, 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment