Thanks for the interesting comments!
I wasn't familiar with traits. If I correctly understand what they are, then:
- Traits are like interfaces in that they provide a way for a class to promise it fulfils some {interface, protocol} — that is, to say "I can do this".
- They are like a base class in that the trait itself can provide some implementation code of its own. (Perl traits also allow operator overloading definitions.)
- Unlike interfaces and base classes, but like mixins, all entities are pushed into the consuming module's namespace.
You suggest that traits are better than interfaces because they allow some code reuse; and are safer than multiple inheritance because (I presume) they avoid ordering problems. Since I'm new to this and haven't tried it out yet: can a class that consumes a trait override the code it receives from the trait? (Is this definable formally? Is it encouraged?) Apart from moving them to deterministic compile-time, how do traits help resolve ordering problems associated with MI? Do you simply get compiler errors when redefining subs/interfaces? What means does the programmer have to resolve these conflicts? (I saw something called "aliases", but if that's a proposed solution to the problem then I'm not sure how it works; I thought the strength of interfaces was that foreign code knows precisely which method to call and can assume that method fulfils a particular interface.)
Also, an implementation question. Suppose I have class Base that uses traits. Then along comes class Child. Where does it get its traits from? Presumably from the parent class, right? But then the traits aren't truly mixed in. The main problem with that is that it gets hard to specify (and implement) what should happen when Child consumes another trait of its own, that conflicts with something from Base.
I'm new to this, so please go gentle if there are obvious answers to these questions :)
(I planned the original post because I liked the clever implementation described, not because I wanted to advocate the use of interfaces; but if a language debate springs up all the better! There's a lot for me to learn here.)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.