Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write the function isCaseObject in scala?

Tags:

scala

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
like image 466
Julio Faerman Avatar asked Dec 16 '22 21:12

Julio Faerman


1 Answers

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)
  }

}
like image 109
Dominik Avatar answered Dec 31 '22 14:12

Dominik