A method trait, defined with trait_mod, is flawlessly inherited from a class defined in an other file. This doesn't seem to be working when the two classes are defined in the same file.
The 2 following files are working fine together:
# mmm.pm6
class TTT is export {
multi trait_mod:<is> (Routine $meth, :$something! ) is export
{
say "METH : ", $meth.name;
}
}
# aaa.p6
use lib <.>;
use mmm;
class BBB is TTT {
method work is something { }
}
Output is: METH : work
But the same code gathered in the following unique file gives an error message
# bbb.p6
class TTT is export {
multi trait_mod:<is> (Routine $meth, :$something! ) is export
{
say "METH : ", $meth.name;
}
}
class BBB is TTT {
method work is something { }
}
Output is:
===SORRY!=== Error while compiling /home/home.mg6/yves/ygsrc/trait/bbb.p6
Can't use unknown trait 'is' -> 'something' in a method declaration.
at /home/home.mg6/yves/ygsrc/trait/bbb.p6:12
expecting any of:
rw raw hidden-from-backtrace hidden-from-USAGE pure default
DEPRECATED inlinable nodal prec equiv tighter looser assoc
leading_docs trailing_docs
I'm running this Rakudo version:
This is Rakudo Star version 2019.03.1 built on MoarVM version 2019.03
implementing Perl 6.d.
Why can't I run this code from a single file ? What did I miss ?
According to the documentation:
Module contents (classes, subroutines, variables, etc.) can be exported from a module with the
is export
trait; these are available in the caller's namespace once the module has been imported withimport
oruse
.
You can try using import
to import the is
trait into the current package:
bbb.p6:
module X { # <- declare a module name, e.g. 'X' so we can import it later..
class TTT is export {
multi trait_mod:<is> (Routine $meth, :$something! ) is export
{
say "METH : ", $meth.name;
}
}
}
import X;
class BBB is TTT {
method work is something { }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With