Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7025579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:59:35+00:00 2026-05-27T23:59:35+00:00

Consider these two traits: trait Poked extends Actor { override def receive = {

  • 0

Consider these two traits:

trait Poked extends Actor {
  override def receive = {
    case Poke(port, x) => ReceivePoke(port, x)
  }

  def ReceivePoke(port: String, x: Any)
}

trait Peeked extends Actor {
  override def receive = {
    case Peek(port) => ReceivePeek(port)
  }

  def ReceivePeek(port: String)
}

Now consider I can create a new Actor that implements both traits:

val peekedpoked = actorRef(new Actor extends Poked with Peeked)

How do I compose the receive handlers? i.e., the receiver should be something like the following code, though “automatically generated” (i.e., all traits should compose):

def receive = (Poked.receive: Receive) orElse (Peeked.receive: Receive) orElse ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:59:36+00:00Added an answer on May 27, 2026 at 11:59 pm

    You can use super[T] to reference members of particular super classes/traits.

    For example:

    trait IntActor extends Actor {
        def receive = {
            case i: Int => println("Int!")
        }
    }
    
    trait StringActor extends Actor {
        def receive = {
            case s: String => println("String!")
        }
    }
    
    class IntOrString extends Actor with IntActor with StringActor {
        override def receive = super[IntActor].receive orElse super[StringActor].receive
    }
    
    val a = actorOf[IntOrString].start
    a ! 5 //prints Int!
    a ! "Hello" //prints String!
    

    Edit:

    In response to Hugo’s comment, here’s a solution that allows you to compose the mixins without having to manually wire their receives together. Essentially it involves a base trait with a mutable List[Receive], and each mixed-in trait calls a method to add its own receive to the list.

    trait ComposableActor extends Actor {
      private var receives: List[Receive] = List()
      protected def registerReceive(receive: Receive) {
        receives = receive :: receives
      }
    
      def receive = receives reduce {_ orElse _}
    }
    
    trait IntActor extends ComposableActor {
      registerReceive {
        case i: Int => println("Int!")
      }
    }
    
    trait StringActor extends ComposableActor {
      registerReceive {
        case s: String => println("String!")
      }
    }
    
    val a = actorOf(new ComposableActor with IntActor with StringActor).start
    a ! 5 //prints Int!
    a ! "test" //prints String!
    

    The only thing to keep in mind is that the order of the receives should not be important, since you won’t be able to easily predict which one is first in the chain, though you could solve that by using a mutable hashmap instead of a list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider these two abstract classes. MyClass2 extends my BaseClass and overrides a virtual method.
Consider these two function definitions: void foo() { } void foo(void) { } Is
Consider these two classes mapped to the same table. One is readonly via mutable=false.
I ran across a compilation issue today that baffled me. Consider these two container
Consider these two functions: 'Overload 1 <Extension()> Function ComputeReturnValue(Of TSource, TResult)(ByVal source As TSource,
How does EmailProperty differ from StringProperty ? Consider these two examples: # example 1:
Consider these two snippets: try: a+a=a except SyntaxError: print first exception caught . try:
Consider these two structures: struct Task { public Int32 Id; public String Name; public
Consider these two tables: CREATE TABLE GAME_ROUNDS( RoundId int primary key NOT NULL, GameId
Consider these two resources: user and group . Rules: A group is owned by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.