(I think you mean Roles, not Traits -- in Perl 6 terms, there's a big difference.)

You're right in that viewing a role as a gussied up export works, but it works in the same way that you can write OO code in C. Sure, you have to maintain your list of methods appropriately and pass around the invocant manually, but you can get some of the benefit with a bit of clever work.

What you're missing is that the roles that apply to a class actually affect the type system. That is, you can write a method which expects an argument that does the Stringify role -- because you're going to print it. The callers of that method don't have to coerce their arguments into new, temporary String objects because you didn't require that that argument be a string, just that it does something sensible when you treat it as a string.

Of course, you do get the benefits of code-reuse without having to maintain a fragile and usually precarious class hierarchy with the plain-Exporter approach.


In reply to Re: Roles as Method Exporters by chromatic
in thread Traits as Method Exporters by simonm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.