Hello!

If roles are interfaces, do we want any class that provides an interface consistent with a role to implicitly do the role? That is, if a class fulfills all of the interface requirements of a role without actually saying it does the role, does it do the role anyway?

    role Documented {
         has $.documentation;
    }

    class Foo does Documented { ... }

    # I don't know why Bar isn't declared as doing Documented
    # but it meets all the requirements of doing Documented...
    class Bar {
        has $.documentation;
    }

    my $baz = Foo.new( $documentation => "q to quit" );
    my $qux = Bar.new( $documentation => "enter to continue" );

    sub show_docs ( $obj.does( Documented ) )  { # or $obj ~~ Documented
        say $obj.documentation;
    }

    show_docs( $baz ); # "q to quit"
    show_docs( $qux ); # "enter to continue" -or-
                       # error because Bar doesn't explicitly do
                       # Documented?


I guess we don't really need implicit doing of roles to get both of those objects to print their documentation, as we could have something like

    sub show_docs ( $obj ) {
        if ( defined $obj.documentation ) {
            say $obj.documentation;
        }
        else {
            die "Should have included a documentation string"
        }
    }


Should roles that are implicitly done pass tests like .does and ~~ ? What are the reasons that they should or shouldn't?

Thanks,
/au

Reply via email to