How to check which case class parameters have a default value using scala reflection 2.10

My task is to find case class parameter names for which there are default values.

In 2.9, I used ScalaSigParser from Skype and did something similar to:

(...) case x: MethodSymbol if x.name.startsWith("init$default$") => (...) (...) 

I was hoping that the reflection in 2.10 would give me easier access to this information.

In the end, I would like to write a macro that I would include in the companion class class, which will automatically generate code to serialize / deserialize this case class. To do this, I need to know which parameters have default values.

+8
macros reflection scala
source share
1 answer

There is currently no way to do this, however I just sent a transfer request (https://github.com/ scala / scala / pull / 1047) that adds TermSymbol.isDefaultParam that provides the requested functionality. Hopefully he will switch to RC1 and 2.10.0-final.

 scala> case class C(x: Int, y: Int = 2) defined class C scala> val ctor = typeOf[C].declaration(nme.CONSTRUCTOR).asMethod ctor @ 39fe9830: reflect.runtime.universe.MethodSymbol = constructor C scala> ctor.params.flatten filter (_.asTerm.isDefaultParam) res0 @ 7ad2093b: List[reflect.runtime.universe.Symbol] = List(value y) 
+3
source share

All Articles