Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect case class in scala macro

Within a method being called as a scala (2.11) macro, is there a way to programmatically determine whether a Type is a case class or not?

The API for the method I'm working through boils down to this:

def typeIsCaseClass(c: Context)(targetType: c.universe.Type): Boolean = {
  // targetType "is case class?"
}

I'm open to altering the API if need be.

like image 415
eouw0o83hf Avatar asked Dec 01 '25 05:12

eouw0o83hf


1 Answers

The symbols usually contain all the interesting information:

def typeIsCaseClass(c: Context)(targetType: c.universe.Type): Boolean = {
  val sym = targetType.typeSymbol
  sym.isClass && sym.asClass.isCaseClass
}
like image 53
Jasper-M Avatar answered Dec 02 '25 19:12

Jasper-M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!