9
votes

I need to have a role with a little bit of special functionality implemented through a Metamodel class of mine inheriting from Metamodel::ParametricRoleHOW. One way to apply it is by using a trait. But I want a nice syntax sugar like we can do with classes:

special Mine {
    ...
}

Ok, I add the following into my module:

my package EXPORTHOW {
    package DECLARE {
        constant special = My::Metamodel::SpecialRoleHOW;
    }
}

and everything runs smoothly... No, it is not:

class Foo does Mine {
    ...
}

and I end up with:

Could not instantiate role 'Mine':

A very brief inspection of Actions.nqp revealed that roles have special handling, but only and only when role keyword is used for package declaration. Hence, I conclude that the only way to have the syntax sugar – is to mixin my own rule into the main grammar and simulate the role declaration. Luckily, package_def relies upon $*PKGDECL and it makes such simulation possible.

Am I right in the conclusion? Or a simpler way exists to achieve the goal?

1
I'm not sure I understand the question... You are asking for the syntax to declare a role-like thingie? Because I'm not sure I follow the path from that Mine to your use of EXPORTHOW::DECLARE...jjmerelo
You got it right. Unfortunately, since asking the question I found the answer and it's: no, this won't work. Roles are handled specially by the grammar. Unfortunately, this is done by checking package keyword, not archetype. So, if a role is declared with something else besides role – it won't act properly. On IRC Jonathan confirmed this and said that he would change this behavior when have a chance. I'm working on a slang workaround for this meanwhile.Vadim Belman
you can try and answer it yourself below. Better than still show up as an unanswered question :-)jjmerelo
Done. Thanks for noting on this!Vadim Belman

1 Answers

3
votes

The requested functionality is not yet possible. But AFAIK, Jonathan Worthington is working on code which would derive a package type from its metaclass. Not sure as to how far is he on this though.

Meanwhile, the desired functionality could be achieved through run time extending of Perl 6 grammar. I have it done for OO::Plugin module.