This is an excerpt from a larger body of code with non pertinent code removed for brevity. I’ve tested the code below on it’s own (as a single class in a new project) and verified that the same exact issue persists. So I know that the problem is specific to the code contained here.
object HumanGUI extends SimpleGUIApplication with Logs {
PropertyConfigurator.configure("log4j.properties")
def top = new MainFrame {
title = "BJ GUI"
val PlayPanel = new BoxPanel(Orientation.Vertical) {
val hitButton = new Button("Hit")
val stayButton = new Button("Stay")
val doubleButton = new Button("Double")
val quitButton = new Button("Quit")
contents += hitButton
contents += stayButton
contents += doubleButton
contents += quitButton
listenTo(hitButton, stayButton, doubleButton, quitButton)
reactions += {
case ButtonClicked(hitButton) =>
debug("Clicked Hit!")
case ButtonClicked(stayButton) =>
debug("Clicked Stay!")
case ButtonClicked(doubleButton) =>
debug("Clicked Double!")
case ButtonClicked(quitButton) =>
debug("Clicked Quit!")
}
contents = new BoxPanel(Orientation.Vertical) {
contents += playPanel
}
}
Specifically, the compiler is telling me that the last 3 cases in this block are unreachable:
listenTo(hitButton, stayButton, doubleButton, quitButton)
reactions += {
case ButtonClicked(hitButton) =>
debug("Clicked Hit!")
case ButtonClicked(stayButton) =>
debug("Clicked Stay!")
case ButtonClicked(doubleButton) =>
debug("Clicked Double!")
case ButtonClicked(quitButton) =>
debug("Clicked Quit!")
}
Why would this be the case?
When pattern matching all lower case variables will be bound to what is matched in that case, if you want to match against a variable outside of a pattern match you have to use ` (back tick):