How can i write a isCaseObject function in scala, so that this works:
def isCaseObject(x:Any) = /* Some Code */
case object aCaseObject
println(isCaseObject(aCaseObject)) //true
println(isCaseObject("not a case object")) //false
Using reflection (Scala 2.10M4), this can be done as follows:
import scala.reflect.runtime.universe.Flag
import scala.reflect.runtime.{ currentMirror => cm }
object Check {
def isCaseObject(x:Any): Boolean = {
cm.reflect(x).symbol.hasFlag(Flag.CASE)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With