2.10 reflection: Getting values and variables.

402 views
Skip to first unread message

Sean Parsons

unread,
May 6, 2012, 11:32:35 AM5/6/12
to scala...@googlegroups.com
I can't see anything on the instance of Symbol that I get from TypeTag.tpe.declarations that I can use to determine if something is a value or variable. It would appear however that the underlying class scala.reflect.internal.Symbols.TermSymbol does provide isValue and isVariable, the package name indicates it shouldn't be used.

Does anyone know what the best approach for this would be? Fundamentally I'm looking for all the fields as they would be in Java terms, but as I'm doing this in a macro implementation it does not appear that I have access to those Java types.

Eugene Burmako

unread,
May 6, 2012, 1:49:53 PM5/6/12
to scala-user
Check `modifiers`.

scala> class A { val a = 2; var b = 3 }
defined class A

scala> import scala.reflect.mirror._
import scala.reflect.mirror._

scala> classToType(classOf[A]).declarations.filter(!_.isMethod).toList
res8: List[reflect.mirror.Symbol] = List(value a, variable b)

scala> res8(0).modifiers
res9: Set[scala.reflect.api.Modifier] = Set(private, local)

scala> res8(1).modifiers
res10: Set[scala.reflect.api.Modifier] = Set(mutable, private, local)

Eugene Burmako

unread,
May 8, 2012, 5:32:50 PM5/8/12
to scala-user
I also added `isValue` and `isVariable` flags.

The first one is true, whenever we have a term symbol, which is
neither a method nor a module.

The second one is true whenever the first one is true + the term is
mutable.

On May 6, 5:32 pm, Sean Parsons <seantpars...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages