我正在尝试设计几个继承部分函数的类,但我似乎无法正确地获得语法。我的超类看起来像这样:
abstract class Controller {
val react:PartialFunction[Event,Unit]
}
子类看起来像:
class BoardRendererController(val renderer:BoardRenderer, val board:Board) extends Controller {
override val react {
case PieceMovedEvent(piece, origin, destination) => println("Moving now")
}
}
但是这个错误无法编译
[ERROR] /workspace/pacman/src/main/scala/net/ceilingfish/pacman/BoardRendererController.scala:14: error: '=' expected but '{' found.
[INFO] override val react {
[INFO] ^
[ERROR] /workspace/pacman/src/main/scala/net/ceilingfish/pacman/BoardRendererController.scala:17: error: illegal start of simple expression
[INFO] }
[INFO] ^
我已尝试过大量的变体,有人知道正确的语法是什么吗?
答案 0 :(得分:7)
除了abhin4v的简洁建议之外,您还必须在定义中提供类型注释,因此我建议您在基类中添加此类注释:
type PFEU = PartialFunction[Event, Unit]
然后你的子类看起来像这样:
class BoardRendererController(val renderer:BoardRenderer, val board:Board)
extends Controller
{
override val react: PFEU = {
case PieceMovedEvent(piece, origin, destination) => println("Moving now")
}
}