Scala:密封特征对象在案例声明中不起作用:像演员一样

Scala: sealed trait objects are not working inside case statement: Akka actors

我一直在努力处理以下与密封特征对象使用相关的错误,以防万一 statement.This 是我与 akka 相关的代码 actors.I 我正在使用伴随对象并定义密封特征,如图所示在下面的 scala 文件中。

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
}

这是我在相应 类.

的伴生对象中定义密封特征对象后得到的错误堆栈
[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
>

供您参考,这里是 build.sbt

name := "akka-actors"

version := "1.0"

scalaVersion := "2.11.8"

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

您只需将伴随对象中的那些对象导入到演员中,这里是 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.....")
  }
}

或者,您可以将导入语句添加到文件顶部,以便所有内容都可以访问它们:

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