I have this simple code:
❯ scala-cli -j system -S 3.3.4
Welcome to Scala 3.3.4 (21.0.5, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
scala> case class Point(x:Int,y:Int)
// defined case class Point
scala> val p = Point(1,2)
val p: Point = Point(1,2)
scala> Point.unapply(p)
val res0: Point = Point(1,2)
After having read this and the official doc, I expect that Point.unapply()
to return Option[(Int, Int)]
instead of being an identity function. In my example to return Some((1, 2))
.
What am I missing in my reasoning?
Empirically, I’ve discovered a different behaviour in Scala 3.X vs Scala 2.13
❯ scala-cli -j system -S 2.13.15
Downloading Scala 2.13.15 bridge
Welcome to Scala 2.13.15 (OpenJDK 64-Bit Server VM, Java 21.0.5).
Type in expressions for evaluation. Or try :help.
scala> case class Point(x:Int,y:Int)
class Point
scala> val p = Point(1,2)
|
val p: Point = Point(1,2)
scala> Point.unapply(p)
val res0: Option[(Int, Int)] = Some((1,2))
Explanation:
https://docs.scala-lang.org/scala3/reference/changed-features/pattern-matching.html
3