I have the following case class with a default parameter and I’m wondering how I can write an unapply method so that I can just extract the first two parameters.
I hope the code below is clear.
case class Point(x: Double, y: Double, _key: Option[String] = None) {
def key: String = _key.getOrElse("")
}
object Point {
def unapply(p: Point) = (p.x, p.y)
}
// pSeq is Seq[Point]
pSeq.map { case Point(x,y) => x + y } // This causes a compiler error:
// wrong number of arguments for <none>:
// (x: Double, y: Double, _key: Option[String])
I’m not sure if this is what you are looking for, but it would give you the API that you describe.
I think that just using a wildcard in the case class is preferred if that will work for you.