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 }
source share