Quick actions

cmd+k|ctrl+k

Navigation

Languages

problem with redispatch

Snippet info

Language

Raku

Visibility

public

Author

jack98765.2007

Created

2023-02-19T13:14:36.992479Z

Updated

2023-03-04T14:37:36.413172Z

class A {}
class A_ is A {}

class B {
    multi method f(A $) {
        say 'B: hi A';
    }
    
    multi method f(A_ $) {
        say 'B: hi A_';
        nextsame;
    }
}

# using redispatching

class C is B {
    multi method f(A $) {
        say 'C: hi A';
        nextsame;
    }
    
    multi method f(A_ $) {
        say 'C: hi A_';
        nextsame;
    }
}

C.f(A_);

say();

# using super

class SuperDelegator {
    has $!object is built;
    has %!super-methods = $!object.^mro[1].^method_table;
    
    method FALLBACK($name, |c) {
        %!super-methods{$name}($!object, |c);
    }
}

sub super($o) {
    SuperDelegator.new(object => $o);
}

class D is B {
    multi method f(A $a) {
        say 'D: hi A';
        super(self).f($a);
    }
    
    multi method f(A_ $a) {
        say 'D: hi A_';
        super(self).f($a);
    }
}

D.f(A_);
INFO