Let me see if I can channel @raiph and answer this question in the best way possible. There are several errors here.
Using import
The main use case for import
is exactly the one you do here; it's described in the documentation. However, it's a better practice to put modules in different files (with the same name as the module) and go for use
instead. use
which imports and includes everything into the namespace. use
loads (via need
) and then imports the module exactly once.
Save curly braces using unit
unit
is basically syntactic sugar to save braces. So the first two files will become:
Foo.pm6
unit module Foo;
role foo is export {
}
Bar.pm6
unit module Bar;
use Foo;
role bar is export does foo {}
The use
of Foo
imports the foo
symbol directly, and it can be used as a role.
What is a role
Last but not least, a role mixed in does is literally a "does" declaration. It's not an "is-a" declaration. So bar
can do
foo, but it's not foo
. Which makes the last file something like this:
use-foo-bar.p6
:
use Foo;
use Bar;
sub f(bar \k) { }
f(bar.new);
Note that I have used bar \k
instead of foo \k
. If I didn't, the error would be: Type check failed in binding to parameter 'k'; expected Foo::foo but got Bar::bar (Bar::bar.new)
Back to the original post
What was wrong was simply the sub declaration, which should have been:
sub f(bar \k) { }
Due to what is explained in the last section above. However, I needed to examine a bit the rest of the program to find this out.