3

I'm writing a Scala macro and am traversing the tree to find non-private fields in classes.

Consider this code that the macro looks at:

class Foo {
  val bar: String = "test"
}

I'm traversing this code and getting to bar's ValDef. It has only two flags in its modifiers: Flag.PRIVATE and Flag.LOCAL.

Using the private modifier on bar changes nothing. Using the protected modifier only adds Flag.PROTECTED to the list of flags.

What am I missing? How do I make the distinction between private and public fields?

Edit:

The following code:

val bar: String = "test"

Has neither Flag.PRIVATE nor Flag.LOCAL, which makes sense since it's a 'global' public val.

The context I'm working inside is writing a new wart for wartremover, which simply takes a Traverser from the context's universe when expanding the macro and traverses over the block of code.

Omer van Kloeten
  • 11,800
  • 9
  • 42
  • 53

1 Answers1

2

A val definition in Scala expands to a private[this] field with an additional getter. Other than the ValDef you're seeing there should be an additional DefDef method definition with the same name which is the getter on the field.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321