Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I create a component-based message handling system with traits?
    primarykey
    data
    text
    <p>Here's a short example of what I want to do:</p> <pre><code>abstract class Message() case class FooMessage() extends Message case class BarMessage() extends Message //... other messages ... trait Component { def handleMessage(msg: Message):Unit } trait ComponentType1 extends Component { abstract override def handleMessage(msg: FooMessage) = { //handle foo, pass it up the chain super.handleMessage(msg) } abstract override def handleMessage(msg: BarMessage) = { //handle bar, pass it up the chain super.handleMessage(msg) } } //handles some other messages, also might handle the same messages as ComponentType1 trait ComponentType2 extends Component { .. } </code></pre> <p>Then, these <code>ComponentType</code>s are <strong>mixed in</strong> to a class to form an object that is completely composed of modular components.</p> <p>I have a bunch of different <code>Message</code>s and a bunch of different <code>Component</code>s.</p> <ul> <li>Not all components handle all message types.</li> <li>Multiple components can handle the same message type. <ul> <li>The message cascades up through the components, even if it's handled by another component.</li> </ul></li> <li>A Component can handle more than one message type.</li> </ul> <p>The problem is since <code>handleMessage</code> is defined in <code>Component</code> as accepting a <code>Message</code>, when I try to specialize the <code>msg</code> parameter it doesn't count as an override.</p> <p>I know one possible solution to this is to declare a big <code>handleMessage</code> method with a big match statement, but I'd like to define a method per message if possible.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload