How do I write a scala extractor for a case class with default parameters?

case-class, extractor, scala

Solution

I'm not sure if this is what you are looking for, but it would give you the API that you describe.

sealed abstract class Point(x: Double, y: Double)
case class PointKey(x: Double, y: Double, _key: String) extends Point(x,y)
case class PointNoKey(x: Double, y: Double) extends Point(x,y)
object Point {
  def apply(x: Double, y: Double) = PointNoKey(x,y)
  def apply(x: Double, y: Double, _key: String) = PointKey(x,y,_key)
  def unapply(p: Point): Option[(Double,Double)] = p  match {
    case PointNoKey(x,y) => Some(x,y)
    case PointKey(x,y,_) => Some(x,y)
  }
}

I think that just using a wildcard in the case class is preferred if that will work for you.

pSeq.map { case Point(x,y,_) => x + y }

Problem

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]) ```

Original source