Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Thanks for the interesting comments!

I wasn't familiar with traits. If I correctly understand what they are, then:

  1. 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".
  2. 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.)
  3. 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.)


In reply to Re^2: interface.pm explained by gaal
in thread interface.pm explained by gaal

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2024-04-23 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found