Im looking to create a pattern matcher which doesnt require value extraction, and havent found a very satisfying way of doing this. Suppose I have the following illustrative example:
I want to match on Any being an Int
a:Any match {
case IsAnInt => println(s"$a is an int")
}
Now the closest I can get is either boolean
a:Any match {
case IsAnInt(true) => println(s"$a is an int")
}
object IsAnInt {
def unapply(a:Any):Option[Boolean] = Some(a.isInstanceOf[Int])
}
or unit
a:Any match {
case IsAnInt(()) => println(s"$a is an int")
}
object IsAnInt {
def unapply(a:Any):Option[Unit] = if (a.isInstanceOf[Int]) Some(()) else None
}
Which I can do, but isnt nearly as cool... Is there any trick Im not aware of to achieve the first case?
(just to clarify the example is a simplification, I'm not looking for ways to identify an int)