开发者

Fields interfering with method resolution for Scala Dynamic trait?

开发者 https://www.devze.com 2023-03-09 21:34 出处:网络
As my firs开发者_StackOverflowt foray into Dynamic Scala land, I thought that I\'d try accessing bean properties via applyDynamic.

As my firs开发者_StackOverflowt 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?


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.


Answer on the Scala-Lang mailing list from Martin Odersky

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

0

精彩评论

暂无评论...
验证码 换一张
取 消