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

  • SEARCH
  • Home
  • 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 7859401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:54:06+00:00 2026-06-02T21:54:06+00:00

Could you please explain why the following code is not working as expected. The

  • 0

Could you please explain why the following code is not working as expected.

The actor is not printing the message

Thanks.

class Base {
  def f() = { "This is Base" }
}

class Sub extends Base {
  override def f() = { "This is Sub" }
}

case class myCase(x: Base)

import scala.actors._

object myActor extends Actor {

  def act()
  {
    loop {
      react {
        case myCase(x) => x.f()
        case msg => "unknown"
      }
    }
  }

  def main(args: Array[String]): Unit = {
    this ! myCase(new Base)
    this ! myCase(new Sub)
  }
}
  • 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-06-02T21:54:06+00:00Added an answer on June 2, 2026 at 9:54 pm

    Answering the question

    The problem has nothing whatsoever to do with inheritance or case classes (I have re-titled the question to reflect this). The code does not print anything for two reasons:

    1. Because your code does not actually make any calls to println! Replace:

      case myCase(x) => x.f()
      

      with

      case myCase(x) => println( x.f() )
      
    2. Because you do not start your actor. I think your program would make more sense if the actor were an inner class:

      object myActor extends App {
      
        class MyActor extends Actor {
          def act() {
            loop {
              react {
                ...             // <-- You need to print stuff
              }
            }
          }
        }
      
        val a = new MyActor
        a.start()             // <-- You need to start it
        a ! myCase(new Base)
        a ! myCase(new Sub)
      }
      

    Advice: case classes and inheritance

    I would, however, offer the advice that using inheritance in the presence of case classes is a bad idea. I usually use the approach of declaring common behaviour/state in a trait:

    sealed trait Base {
      def f(): Unit
    }
    
    case class Sub() extends Base
    

    Why is it a bad idea? Well, one of the contracts that case-classes give you is a rigid definition of equivalence (that is, an equals and hashCode implementation). In the presence of inheritance, this could well be misleading. That is, your code will probably not do what you expect. Consider the following;

    scala> abstract class Base { val x: Int }
    defined class Base
    
    scala> case class Sub(s: String) extends Base { val x = util.Random.nextInt(100) }
    defined class Sub
    

    Now if I create 2 instances…

    scala> Sub("Hey")
    res2: Sub = Sub(Hey)
    
    scala> Sub("Hey")
    res3: Sub = Sub(Hey)
    

    They are equivalent

    scala> res2 == res3
    res4: Boolean = true
    

    But they do not have the same state

    scala> res2.x
    res5: Int = 28
    
    scala> res3.x
    res7: Int = 15
    

    Note, I am not saying this is a bug. I’m just saying it’s an area where you might find that you introduce a bug in your code because you have made the assumption that any state of the case class is included in its equivalence.

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

Sidebar

Related Questions

Could anyone please explain the following line of code, found on http://docs.openttd.org/ai__cargo_8cpp_source.html return (AICargo::TownEffect)::CargoSpec::Get(cargo_type)->town_effect;
Could someone please explain the difference between the following two lines of code: 1.
Could you please explain why this code is not syntactically correct? private void addEditor(final
could someone please explain why the following code is throwing an error? // JavaScript
Could someone please explain to me what the following lines of code do? dynamic
Could you please explain why the shell redirection doesn't work with System.Diagnostics.Process class? I
Could you please explain me the reason of the following situation. Today I wrote
Can someone please explain what does the following line of code exactly do? If
I'm very new to boost::spirit/fusion. Could someone please explain to me why the following
Could someone please explain when would I want to use delegation instead of inheritance?

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.