Duncan McGregor
Duncan McGregor

Reputation: 18167

Fields interfering with method resolution for Scala Dynamic trait?

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?

Upvotes: 4

Views: 222

Answers (2)

jsalvata
jsalvata

Reputation: 2205

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.

Upvotes: 2

Duncan McGregor
Duncan McGregor

Reputation: 18167

Answer on the Scala-Lang mailing list from Martin Odersky

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

Upvotes: 0

Related Questions