Why doesn't scala map implement unapply?
I wrote the following usage example in scala:
val wordShortcut = Map("volume" -> "vol", "report" -> "rpt", ...)
object WordShortcutCase {
def unapply(key: String): Option[String] = wordShortcut.get(key)
}
val pluralR = "(.+)s".r
def encodeToken(token: String) = token match {
case WordShortcutCase(short) => short
case pluralR(singular) => singular
case _ => token
}
if scala Mapwill implement unapply, I don't need an extra object WordShortcutCase(I could use it instead case wordShortcut(short) => short). This seems to me a common model.
And so the question is, why does scala Mapnot implement the method unapply?
Mapdoes not implement unapply, because there is no reasonable implementation that has the same characteristics as other collections.
, , , , apply unapply . , ; , ( "" ):
val xs = List("fish")
val ys = List("fish", "dish")
def iam(zs: List[String]) = zs match {
case List(x) => println(s"I am a $x")
case _ => println("Who am I??")
}
iam(xs) // Prints 'I am a fish'
iam(ys) // Prints 'Who am I??'
Map , unapply apply, ( , , ). , , , - ; , unapplySeq , , , . .