无法访问伴随对象案例对象

时间:2017-09-16 09:45:04

标签: scala

我正在使用scala 2.12,我尝试了一段在2.11版本中编码的代码,它不再编译了:

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

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

我有以下编译错误:

not found: value Play
[error]         case Play =>
not found: value Stop
[error]         case Stop =>

1 个答案:

答案 0 :(得分:1)

您需要导入MusicController的案例对象

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

// Music Controller
class MusicController extends Actor {
    import MusicController._ // <-- IMPORT THIS
    def receive = {
        case Play =>
            println("Music Started ...")
        case Stop =>
            println("Music Stopped ...")
    }
}