I guess I independently invented this idea a few years back, but instead of the word 'trait' I used the word 'quality.' I use the term 'attach' for adding a quality to a given object instance, because I also support removing (via 'detach') qualities at runtime also. My qualities do support state information, which does constrain the object model to support that.

(I'll be digging into the linked PDF and other folks' ideas here to see how I can improve my own model. Thanks!)

My implementations in Java supported runtime attachment and detachment of qualities, but my draft Perl implementation has been a little heavy and inelegant so far.

These are very handy in design of various roleplaying games; you can add the "edible" quality/trait to an object and it now supports the verb 'eat.'

If classes are nouns, qualities/traits are adjectives.

Once you get used to the idea of qualities/traits, your class hierarchy is a lot different: most classes are really just groups of qualities without much to do directly. For example, a typical player-character in a MUD, just after drinking a magical potion, might look like:

class Human is Mammal with Bipedal, Sentient, Inventoried, Skilled, Clothed instance 3453 is Human with Character, Flying, Enchanted

After the flying-potion enchantment wears off, just remove Enchanted, which in turn can remove Flying.

--
[ e d @ h a l l e y . c c ]


In reply to Re: Class::Trait to the CPAN? by halley
in thread Class::Trait to the CPAN? by Ovid

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.