Guest User

Untitled

a guest
Jun 25th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. my %A = { isa => "A" };
  2. %A<new> = sub {
  3. my %r;
  4. %r<prototype> := %A;
  5. %r<x> = 5;
  6. return %r;
  7. };
  8.  
  9. %A<foo> = sub ($this) {
  10. my $class = dispatch($this, 'isa');
  11. return "{$class}::foo {$this<x>}";
  12. };
  13.  
  14. sub dispatch(%obj, $call, *@a, *%b) {
  15. my %ref = %obj;
  16. loop {
  17. if %ref{~$call} {
  18. return %ref{~$call}(%obj, |@a, |%b) if %ref{~$call} ~~ Callable;
  19. return %ref{~$call};
  20. }
  21. if %ref<prototype> {
  22. %ref = %ref<prototype>;
  23. }
  24. else {
  25. die 'failed'
  26. }
  27. }
  28. }
  29.  
  30.  
  31. my %a = %A<new>();
  32. say dispatch(%a, 'foo');
  33.  
  34. my %B = { isa => 'B' };
  35. %B<prototype> := %A;
  36. %B<new> = sub {
  37. my %r;
  38. %r<prototype> := %B;
  39. %r<x> = 1;
  40. return %r;
  41. }
  42.  
  43. my %b = %B<new>();
  44. say dispatch(%b, 'foo');
Add Comment
Please, Sign In to add comment