Wow. This was super helpful even to a not-very-newbie. Thanks! Chas.
David Pollak wrote: > > > On Tue, Apr 28, 2009 at 11:07 PM, pravin karne <pravinka...@gmail.com > <mailto:pravinka...@gmail.com>> wrote: > > hi, > i want some explanation for following code > > > object sessionObj extends SessionVar[HashMap[String, Int]]( > new HashMap[String, Int] > { > override def default(key: String): Int = 0 > } > ) > > > Changing a few things that Tim said. > > > > 1. how object extends class in scala.? > > > object creates a singleton... an instance of the class that can be > shared within the scope that it was created. If object is used at the > top-level scope, then it is a singleton shared by all classes in that > classloader (your Lift app.) > > In Scala everything is either an instance of an object or a method. > This means there are no statics, no primitives, etc. In order to have > a singleton instance shared within a classloader, use the object mechanism. > > > > 2. are we going to create object of SessionVar class? > > > Yes, we create a singleton instance of the SessionVar class. The > SessionVar class uses a little machinery to access the backing store of > your session. So, the SessionVar doesn't actually store a value. It is > a proxy that gets the current session based on the context of your > thread, it has a globally unique name and uses this name to look up the > underlying value in a hashmap in the session and then casts it to the > type of the specific SessionVar... in this case HashMap[String, Int]. > In this way, you have type-safe access to session data. This is a > material advance over the way session data is stored in a Java > session... there's no casting on the user's part. > > > > 3. are we calling constructor of given class? > > > Yes. However, if you take a look at the SessionVar's constructor, it's > as follows: > > class SessionVar[T](dflt: => T) > > This is a "call-by-name" parameter. So, it's a function rather than a > value. Why do we do this? This allows us to create a new instance of > the default value of the SessionVar each time it's needed. When you > call the is method on the SessionVar, Lift looks to see if the SessioVar > has been defined. If it has not been defined, then the default value > creator function is applied and we get a new default value, the > SessionVar for the current session is set to that default value and the > freshly minted value is returned. > > > > 4. is it similar to java class which extends other class ,and then > create object of subclass? > > > Scala creates a subclass of SessionVar for your singleton. If you do > sessionObj.getClass.getName, it will not be SessionVar, but some > compiler-generated name. See the answer to #1. > > > > > I am beginner to scala and lift. > > Welcome to the community. > > Thanks, > > David > > > > On Tue, Apr 28, 2009 at 7:28 PM, Timothy Perrett > <timo...@getintheloop.eu> wrote: > > > > Try: > > // this gets you whatever is in the session object so add to it here > SessionObj.is > > Do you specifically need to use Java HashMap? If not, seems like > List[(String,Int)] would be more lift-esq. > > Cheers, Tim > > On 28/04/2009 13:30, "pravin" <pravinka...@gmail.com > <mailto:pravinka...@gmail.com>> wrote: > > > > > Hi guys, > > I want to add objects into session scope. > > > > i am using following code : > > > > object sessionObj extends SessionVar[HashMap[String, Int]]( > > new HashMap[String, Int] > > { > > override def default(key: String): Int = 0 > > } > > ) > > > > So as per my understanding :- > > > > 1. Session object is of HashMap[String, Int] type. > > 2. I want to add no of string object into above map so i can > > access them during my session > > > > correct me if i am wrong > > > > > > So please let me know how can i add/remove different > String object > > from session scope with above code snippet > > > > Thanks in advance > > > > > > > > > > > > > > > > > > -- > Lift, the simply functional web framework http://liftweb.net > Beginning Scala http://www.apress.com/book/view/1430219890 > Follow me: http://twitter.com/dpp > Git some: http://github.com/dpp > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~----------~----~----~----~------~----~------~--~---