23 Dec 23:47
Re: [scala] Scala Object Notation ( SCON? )
Luckily, as the code is written its a copy of Roland, not Roland himself. On 12/23/07, Viktor Klang <viktor.klang@...> wrote: > On Dec 23, 2007 11:34 PM, Oscar Picasso <oscarpicasso@...> wrote: > > > We could also have? > > rolandDumas.firstname("Bertrand").age(33) > > > It feels instinctively wrong to have the Roland Dumas named Bertrand. :( > > > > > > > > > I am curious. Where in the lift code are you making these changes? > > > > > > On Dec 23, 2007 5:05 PM, David Pollak < feeder.of.the.bears@...> > > wrote: > > > > > I've been pestering the Scala folks for a way to update just one field > > > in case classes and have a new instance spit out with just that one > field > > > changed for a very long time. > > > > > > It would work like this: > > > val rolandDumas = Person("Roland", "Dumas", 0) // the factory instance > > > val first= rolandDumas.age(43) // a new instance with age 43 > > > val second = rolandDumas.age(56) // yet another instance with age 56 > > > > > > You'll be able to get that functionality with some of the changes I'm > > > making to lift, but baking it into the language would be nicer. > > > > > > > > > On Dec 23, 2007 9:10 AM, Oscar Picasso <oscarpicasso@...> wrote: > > > > > > > Case class was the first thing I thought when reading the beginning of > > > > this tread. > > > > > > > > May it can be a partial answer to Rickard question. > > > > > > > > **case class Person(firstname: String, lastname: String, age: Int) > > > > > > > > But, in a small village in France, they are many Roland Dumas that > > > > have different ages. Typing over and over Person("Roland", "Dumas", > xxx) is > > > > boring. > > > > > > > > So let's create a Roland Dumas maker. > > > > > > > > scala> def rolandDumasMaker = Person("Roland", "Dumas", _: Int) > > > > rolandDumasMaker: (Int) => Person > > > > > > > > Now creating many Roland Dumases is becoming fun. > > > > scala> rolandDumasMaker(45) > > > > res5: Person = Person(Roland,Dumas,45) > > > > > > > > scala> rolandDumasMaker(85) > > > > res6: Person = Person(Roland,Dumas,85) > > > > > > > > Sure, in many languages you can make this kind of factory but I find > > > > it more meaningful and fun the way you can define it in scala. > > > > > > > > > > > > On Dec 23, 2007 9:42 AM, David Pollak <feeder.of.the.bears@...m> > > > > wrote: > > > > > > > > > > > > > > > > > > > On Dec 23, 2007 3:44 AM, David Bernard <david.bernard.31@...m> > > > > > wrote: > > > > > > > > > > > I thinks that with some change in Property you could support a > > > > > > builder approach that support > > > > > > val w = new Widget() > > > > > > .name("MyWidget") > > > > > > .height(10) > > > > > > .children(label, panel, button) > > > > > > > > > > > > > > > Which is what you get with lift components. > > > > > > > > > > But I believe the larger issue is one of mutability and internal > > > > > type representation. > > > > > > > > > > JavaScript has JSON because it is pretty much built on hash tables > > > > > and the built in types (numbers, Strings, dates, booleans, arrays > and > > > > > hashs/objects) are so limited in number that it makes sense to have > special > > > > > syntax for each of them. > > > > > > > > > > Scala, too, has some special notation for building object > > > > > hierarchies: case classes. One can build complex object hierarchies > using > > > > > case classes. In my opinion, they are as easy to read as JSON > objects. > > > > > They are a little more verbose to type, but that's because they're > typed (ah > > > > > hah... a pun.) > > > > > > > > > > One can also do: > > > > > Map('foo -> Array(1,2,3), 'bar -> Array("dog", "cat"), 'baz -> > > > > > Date("12-23-07")) > > > > > > > > > > It's a little more verbose than JSON, but the objects are > > > > > serializable as JSON (okay, I made up the Date thing, but it's not > hard to > > > > > implement.) > > > > > > > > > > If you want the kind of unstructured hash tables you get with > > > > > JavaScript, use Maps/Arrays (or Lists). If you want more structured > stuff, > > > > > where are case classes lacking? > > > > > > > > > > Thanks, > > > > > > > > > > David > > > > > > > > > > > > > > > > > > > > > > > > > > > > my 2 cents. > > > > > > > > > > > > /davidB > > > > > > > > > > > > Andrés Testi 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@...>: > > > > > > >>> 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 > > > > > > >> > > > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > lift, the secure, simple, powerful web framework http://liftweb.net > > > > > Collaborative Task Management http://much4.us > > > > > > > > > > > > > > > > > > > > > -- > > > lift, the secure, simple, powerful web framework http://liftweb.net > > > Collaborative Task Management http://much4.us > > > > > > > > > > -- > _____________________________________ > / \ > /lift/ committer (www.liftweb.net) > SGS member (Scala Group Sweden) > SEJUG member (Swedish Java User Group) > Coffee drinker (Skånerost) > \_____________________________________/ >
RSS Feed