This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Dynamic trait : method v field access

4 replies
Duncan McGregor
Joined: 2011-06-01,
User offline. Last seen 42 years 45 weeks ago.

Sorry to cross-post from scala-used, but I having not had an answer
there this question is probably better targeted here.

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

I know that Dynamic is work in progress, but for my edification - is
this by design, an oversight, or something that is planned to be
fixed?

Thanks

Duncan McGregor

Duncan McGregor
Joined: 2011-06-01,
User offline. Last seen 42 years 45 weeks ago.
Re: Dynamic trait : method v field access

A gentle prod, as I have some real applications of this technique that
I'd like to be able to use.

Thanks in anticipation

Duncan McGregor

On Jun 2, 7:56 am, Duncan McGregor wrote:
> Sorry to cross-post from scala-used, but I having not had an answer
> there this question is probably better targeted here.
>
> 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
>
> I know that Dynamic is work in progress, but for my edification - is
> this by design, an oversight, or something that is planned to be
> fixed?
>
> Thanks
>
> Duncan McGregor

mepcotterell
Joined: 2011-03-17,
User offline. Last seen 40 weeks 6 days ago.
Re: Re: Dynamic trait : method v field access

I wish I could say more than just this is interesting. Hopefully the
bump will help.

Sincerely,
Michael Cotterell
mepcotterell@gmail.com
mepcott@uga.edu

P.S. - Check out ScalaTion (http://code.google.com/p/scalation/), a
Domain-Specific Language for Modeling & Simulation.

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: Dynamic trait : method v field access


On Thu, Jun 2, 2011 at 8:56 AM, Duncan McGregor <oneeyedmen@googlemail.com> wrote:
Sorry to cross-post from scala-used, but I having not had an answer
there this question is probably better targeted here.

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

I know that Dynamic is work in progress, but for my edification - is
this by design, an oversight, or something that is planned to be
fixed?

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

Cheers

 -- Martin

Duncan McGregor
Joined: 2011-06-01,
User offline. Last seen 42 years 45 weeks ago.
Re: Dynamic trait : method v field access

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

Excellent thank you. I get a big grin whenever I think through the
possibilities of Dynamic.

Duncan

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland