Given the following code:
case class ChangeSet(field:String, from:Object, to:Object)
private var changed:List[ChangeSet] = Nil
def change(field:String, from:Object, to:Object) {
changed.find{ case ChangeSet(field,_,_) => true } match {
case Some(ChangeSet(field,to,_)) => // do stuff
case Some(_) => // do stuff
case _ => // do stuff
}
}
The line giving me trouble is Some(ChangeSet(field,to,_)).
It compiles but what seems to be happening is that Scala is filling it in as a placeholder for a wildcard. I base that assumption on the fact that when I do the following Some(ChangeSet(field,to,to)) I get the error to is already defined as value.
What I wanted is to make a ChangeSet object with to from the method parameters.
Is that possible?
When pattern matching Scala interprets all identifiers starting with lower case as placeholders and fills in values. To tell Scala to use
toas a constant value from the outer scope you need to surround it with backticks:`to`. Alternatively, you could change the name oftotoToas Rex Kerr suggested, but I prefer to keep my variables lowercase.This should work: