0
votes

I have been struggling to deal with the below error related to sealed trait object usage in case statement.This is my code related to akka actors.I am using companion Objects and defined the sealed traits as shown in below scala file.

ActorCreation.scala

import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props

object MusicController{
  sealed trait ControllerMsg
  case object Play extends ControllerMsg
  case object Stop extends ControllerMsg

  }

class MusicController extends Actor{
  def receive ={
   case Play =>
     println("Music Started.....")
   case Stop =>
     println("Music Stopped.....")
 }
}

object MusicPlayer{
  sealed trait PlayMsg
  case object StopMusic extends PlayMsg
  case object StartMusic extends PlayMsg
}

class MusicPlayer extends Actor {
  def receive ={
   case StopMusic =>
     println("I don't Stop Music now.....")
   case StartMusic =>
     val controller = context.actorOf(Props[MusicController],"controller")
     controller ! Play
   case _ =>
     println("Unknown Message")
 }
}

object Creation extends App{

 val system = ActorSystem("creation")

 val player = system.actorOf(Props[MusicPlayer],"player")

 player ! StartMusic
}

Here is the error stack i am getting related to sealed trait object after defining them in companion objects for the respective classes.

[info] Updating {file:/C:/Akka%20Actors/chapter02/}chapter02...
[info] Resolving jline#jline;2.12.1 ...
[info] Done updating.
[info] Compiling 1 Scala source to C:\Akka Actors\chapter02\target\scala-2.11\classes...
[error] C:\Akka Actors\chapter02\src\main\scala\ActorCreation.scala:14: not found: value Play
[error]    case Play =>
[error]         ^
[error] C:\Akka Actors\chapter02\src\main\scala\ActorCreation.scala:16: not found: value Stop
[error]    case Stop =>
[error]         ^
[error] C:\Akka Actors\chapter02\src\main\scala\ActorCreation.scala:30: not found: value StopMusic
[error]    case StopMusic =>
[error]         ^
[error] C:\Akka Actors\chapter02\src\main\scala\ActorCreation.scala:32: not found: value StartMusic
[error]    case StartMusic =>
[error]         ^
[error] C:\Akka Actors\chapter02\src\main\scala\ActorCreation.scala:34: not found: value Play
[error]      controller ! Play
[error]                   ^
[error] C:\Akka Actors\chapter02\src\main\scala\ActorCreation.scala:46: not found: value StartMusic
[error]  player ! StartMusic
[error]           ^
[error] 6 errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 10 s, completed Sep 22, 2017 11:12:37 AM
>

For your information , here is build.sbt

name := "akka-actors"

version := "1.0"

scalaVersion := "2.11.8"

libraryDependencies +=
    "com.typesafe.akka" % "akka-actor_2.11" % "2.4.0"
1

1 Answers

2
votes

You just need to import those objects from the companion objects into the actors, here is an example with the MusicController:

object MusicController {
  sealed trait ControllerMsg
  case object Play extends ControllerMsg
  case object Stop extends ControllerMsg

}

class MusicController extends Actor{
  import MusicController._  // <--- Add this

  def receive = {
   case Play =>
     println("Music Started.....")
   case Stop =>
     println("Music Stopped.....")
  }
}

Alternatively you can add the import statements to the top of the file so everything can access them:

import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
import example.MusicPlayer._       // new
import example.MusicController._   // new