Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't inherit, in Raku, a method trait from a class defined in the same file

Tags:

raku

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 ?

like image 727
yguillemot Avatar asked Dec 02 '19 22:12

yguillemot


1 Answers

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 with import or use.

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 {  }
}
like image 95
Håkon Hægland Avatar answered Nov 16 '22 09:11

Håkon Hægland