How to write scala extractor for case class with default parameters?

I have the following case class with a default parameter, and I wonder how I can write an unapply method so that I can just extract the first two parameters.

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) = (px, py) } // 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]) 
+4
source share
1 answer

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

 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 just using a wildcard in the case class is preferable if this works for you.

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

Source: https://habr.com/ru/post/1448046/


All Articles