Parametric Roles

I meant to blog about what I was working on a bit more this week, but have ended up with my head in the code all week instead. The end result of this "codeathon" is that I'm now much further forward on my Hague grant. While some of the smaller tasks scheduled for December remain to be done, I have taken parametric roles from non-existent to doing everything I aimed to get them to do during January and some of the February aspects of them now work too. I've also done some work on the dispatcher refactor, got us distinguishing different routine types and have also used the end result to get submethod dispatch working as it should too. So, some details on the parametric roles.

In Perl 6, roles can take parameters. Roles exist to enable greater re-use of code than we could get through having plain old classes, and by allowing them to be parameterized we open the door to even more re-use. Taking a simple example, imagine we wanted to factor out a "greet" method into a role, which takes somebody's name and greets them. We want to parameterize it on the greeting.

Sadly, the Slovak output sucks here. Borovicka is the nominative form of the word, and we need to decline it into the accusative case. But some languages don't care about that, and we don't want to have to make them all supply a transform. Thankfully, you can write many roles with the same short name, and a different signature, and multi-dispatch will pick the right one for you. So we write something to produce the accusative case in Slovak and pass it in. Here's the new code.

Which means we can now properly order our borovicka in Slovakia, which is awesome. Until you do it in a loop and find the Headache['very bad'] role got mixed into yourself overnight, anyway...

I'll post more on the other bits I'm working on, or have worked on, soon. But parametric roles are the biggy. I'm not at all finished with them yet, but all of the code examples I gave here today now work.:-)

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
Without JavaScript enabled, you might want to
use the classic discussion system instead. If you login, you can remember this preference.

Nice! I didn't notice this module before now, but it appears to capture the concept, just - as you say - without the more compact syntax Perl 6 offers.

It's certainly nice that these abstractions are available for Perl 5 programmers, not least because Perl 5 is useful for doing Real Work now and Perl 6 ain't quite there yet, but also because anyone using them who might look at Perl 6 in the future will have one less thing to learn.:-)