I want to be able to do this:
scala> val Int(i) = "1"
i: Int = 1
But Int
doesn't have an unapply
method.
I found this answer which gives instructions on how to implicitly add a method to an existing object, so I tried it out. The solution they gave works, but unfortunately not for pattern matching. Here's what I have:
object UnapplyInt {
val IntRE = """^(\d+)$""".r
def unapply(v: String): Option[Int] = v match {
case IntRE(s) => Some(s.toInt)
case _ => None
}
}
implicit def int2unapplyInt(objA: Int.type) = UnapplyInt
These test cases are all fine:
val UnapplyInt(i) = "1" // pattern matching with unapply is fine
val i = Int.unapply("1").get // implicit conversion is fine
But the one I want fails:
scala> val Int(i) = "1"
<console>:10: error: object Int is not a case class constructor, nor does it have an unapply/unapplySeq method
val Int(i) = "1"
^
If the implicit conversion works and pattern matching with unapply
works, why doesn't Scala put these two things together for implicit pattern matching?