Have you read the original "traits" paper?

but now how do I write my other classes against this role?
Other code should be written to use only the API provided by the RemoteConnection role, without caring what class it is otherwise. Any type checking should use the role name.

but then I can't easily inherit from both of them in Server and Client.
That sounds like a different problem. Maybe you didn't explain it well? Are you saying that Server and Client each want a different derived class of the Connection, but the Connection may already be different concrete classes?

You need to aggregate rather than further derive. Have the Connection class that you will be deriving from contain a SSH or Socket back-end instance, rather than being derived from it.

In fact, you might not need inheritance in the other place, either. Look at using collaborating instances instead. You can use delegation to make the result just as slick as inheritance rather than having to forward everything all over the place. Delegation can easily behave like inheritance from a run-time specified instance.

See this node for more recent discussion on delegation.


In reply to Re: Inherit from a Role (or something like that) by John M. Dlugosz
in thread Inherit from a Role (or something like that) by elTriberium

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.