raku

How to forward declare a role to be used for an enum?


Running this piece of code

use v6.d;

role DrawSurface { ... };

enum Surface does DrawSurface <Fire>;

role DrawSurface {
    method draw () {
        given self {
            when Surface::Fire { "f" }
        }
    }
}

Produces this error:

===SORRY!=== Error while compiling /tmp/me.raku
No appropriate parametric role variant available for 'DrawSurface':
    Cannot resolve caller (Surface:U); Routine does not have any candidates.  Is only the proto defined?
at /tmp/me.raku:5

How do I write that so that I can do this: Surface::Fire.draw?


Solution

  • Direct solution using multidispatch.

    module DrawSurface {
        enum Surface is export <Fire>;
    
        proto draw (Surface $ ) is export {*}
        multi draw (Fire) {'f'}
    }
    
    import DrawSurface;
    
    say draw Fire;
    
    f
    

    Combination with role.

    role DrawSurface {
        method draw () {drawp self}
    }
    
    enum Surface does DrawSurface <Fire>;
    
    multi drawp(Fire) { 'f' }
    
    say Fire.draw;
    
    f