Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- my %A = { isa => "A" };
- %A<new> = sub {
- my %r;
- %r<prototype> := %A;
- %r<x> = 5;
- return %r;
- };
- %A<foo> = sub ($this) {
- my $class = dispatch($this, 'isa');
- return "{$class}::foo {$this<x>}";
- };
- sub dispatch(%obj, $call, *@a, *%b) {
- my %ref = %obj;
- loop {
- if %ref{~$call} {
- return %ref{~$call}(%obj, |@a, |%b) if %ref{~$call} ~~ Callable;
- return %ref{~$call};
- }
- if %ref<prototype> {
- %ref = %ref<prototype>;
- }
- else {
- die 'failed'
- }
- }
- }
- my %a = %A<new>();
- say dispatch(%a, 'foo');
- my %B = { isa => 'B' };
- %B<prototype> := %A;
- %B<new> = sub {
- my %r;
- %r<prototype> := %B;
- %r<x> = 1;
- return %r;
- }
- my %b = %B<new>();
- say dispatch(%b, 'foo');
Add Comment
Please, Sign In to add comment