4

As my first foray into Dynamic Scala land, I thought that I'd try accessing bean properties via applyDynamic.

My first very rough cut is

trait BeanProperties extends Dynamic {
  def applyDynamic(name: String)(args: Any*) = {
    if (args.length == 0)
      PropertyUtils.getProperty(this, name)
    else
      null
  }
}

so that

val bean = new JTextField("text") with BeanProperties
bean.getText should equal("text")
bean.text should equal("text")

so far so good! But when I try

bean.background should equal(bean.getBackground)

the compiler complains, trying instead to give access to the field named background rather than synthesizing a method.

variable background in class Component cannot be accessed in javax.swing.JTextField with BeanPropertiesTest.this.BeanProperties

Is this by design, an oversight, or something that is planned to be fixed?

Duncan McGregor
  • 17,665
  • 12
  • 64
  • 118

2 Answers2

2

FWIW, I proposed a fix for this https://github.com/scala/scala/pull/98

If it is accepted, it will indeed be fixed by the time Dynamic comes out of -Xexperimental.

jsalvata
  • 2,155
  • 15
  • 32
0

Answer on the Scala-Lang mailing list from Martin Odersky

I think this should be fixed by the time Dynamic comes out of -Xexperimental.

Duncan McGregor
  • 17,665
  • 12
  • 64
  • 118