- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Dynamic trait : method v field access
Thu, 2011-06-02, 07:56
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
Thu, 2011-06-09, 22:37
#2
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.
Fri, 2011-06-10, 14:37
#3
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
Fri, 2011-06-10, 15:57
#4
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
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