22 Dec 23:58
Re: Scala Object Notation ( SCON? )
Stephane Le Dorze <stephane.ledorze <at> gmail.com>
2007-12-22 22:58:54 GMT
2007-12-22 22:58:54 GMT
IMHO; double assignment seems odd for declarative instantiation.
On Dec 22, 2007 5:09 PM, Andrés Testi <andres.a.testi <at> gmail.com> wrote:
Yes, but I don't like the fact of create a subclass of Person. I just
want to instantiate a Person. Suppose you have this Widget class:
class Widget{
val name = new Property("")
val width = new Property(0)
val height = new Property(0)
val children = new Property(Array[Widget]())
}
To populate the Widget properties without Object notation, you need to
write the next code
val w = new Widget
p.name() = "MyWidget"
p.height() = 10
p.children() = Array(
label, panel,button
)
the previous code is not Property-Editor friendly, because the
"programmatic" nature. Then, a declarative syntax is the key. But
creating annonimous class is not declarative but programmatic, because
I would to assign a property 2 times:
new Widget{
name="MyWidget"
name="YourWidget"
}
In the other hand, I don't like to subclass Widget, because I just
want to instantiate Widget.
2007/12/22, martin odersky < martin.odersky <at> epfl.ch>:> > Hello:
> > Is it possible to instantiate scala objects in a declarative way like
> > JSON. I think this would be a solution:
> >
> > val p = new Person{
> > name = "Peter"
> > age = 20
> > friends = Array(
> > new Person{
> > name = "Gary"
> > }
> > )
> >
> > }
> >
> Sure. That you wrote is legal Scala, assuming you declared Person like this:
>
> class Person {
> var name: String = ""
> var age: Int = 99
> var friends: Array[Person] = Array()
> }
>
> But maybe that's not what you wanted?
>
> Cheers
>
> -- Martin
>
RSS Feed