0

I'm trying to design a couple of classes that inherit a partial function, but I don't seem to be able to get the syntax quite right. My superclass looks like this:

abstract class Controller {

    val react:PartialFunction[Event,Unit]

}

And the subclass looks like:

class BoardRendererController(val renderer:BoardRenderer, val board:Board) extends Controller {

    override val react {
        case PieceMovedEvent(piece, origin, destination) => println("Moving now")
    }
}

But this fails to compile with this error

[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] ^

I've tried loads of variations on this, anyone know what the correct syntax is?

1
  • 2
    try override val react = { ... } Commented Jul 7, 2010 at 19:42

1 Answer 1

7

In addition to abhin4v's terse suggestion, you still have to supply a type annotation in the definition, so I recommend this addition to your base class:

type PFEU = PartialFunction[Event, Unit]

Then your subclass would look like this:

class BoardRendererController(val renderer:BoardRenderer, val board:Board)
extends Controller
{
    override val react: PFEU = {
        case PieceMovedEvent(piece, origin, destination) => println("Moving now")
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.