On Nov 12, 1:55 pm, Ferdinand Chan <[email protected]> wrote: > Opyate, > > Thanks for the example. But my scala knowledge seems not good enough > to understand it.
Here's an excerpt from Box's Javadoc (http://scala-tools.org/scaladocs/ liftweb/1.0/net/liftweb/util/Box.html - this is v1.0 so I recommend cloning the latest Lift code and looking at the doc and implementation there): The Box class is a container which is able to declare if it is Full (containing a single non-null value) or Empty. It serves the same purpose as the Option class from Scala standard library but adds several features: * you can transform it to a Failure object if it is Empty (with the ?~ method) * you can chain failure messages on Failure Boxes * you "run" a function on your Box, with a default value: Full(1).run ("zero") { x: String, y: Int => y.toString } * you can "pass" a Box to a function for side effects: Full(1) $ { x: Box[Int] => println(x openOr 0) } So, essentially it's the same as Option, like so: Option : Box Some : Full None : Empty ...but with extra bells and whistles. Then, as you saw, there are various ways to "open" the Box and use what's inside it :-) > > Will try to re-read it couple of times and try to understand it > > Cheers, > > Ferdinand > > On Nov 12, 8:06 pm, opyate <[email protected]> wrote: > > > > > Hey Ferdinand, > > > Here are a couple of examples: > > > import _root_.net.liftweb.common._ > > > val boxExample: Box[Int] => Int = { > > case Full(x) => x > > case Empty => 0 > > } > > > def boxExample2(boxedInt: Box[Int]): Int = { > > boxedInt.map({i => > > i > > }) openOr 0 > > } > > > On Nov 12, 12:28 am, Naftoli Gugenheim <[email protected]> wrote: > > > > What do you want to happen if you call "value" on an Empty? > > > > ------------------------------------- > > > > Ferdinand Chan<[email protected]> wrote: > > > > Wondering what's the normal practice of using a Box. As a Java > > > developer, I always want to get the boxed value by a method named > > > "Value" like > > > > val optionalContent = Full("This is optional") > > > > Log.info( " The optional content is " + optionalContent.value) > > > > But I know its not a valid way to do so in lift ( same for Option in > > > Scala) > > > > From the book programming in Scala , there's an example > > > > val withDefault: Option[Int] => Int = { > > > case Some(x) => x > > > case None => 0 > > > > } > > > > which suggest the use of match to retrieve the value of the boxed > > > content. It make sense to me and I'm totally agree with the advantage > > > of using match + option (Box) combination. > > > > However, as a newbie to Scala, I'm so tempted to use something > > > like .Value to retrieve the box value . > > > > So, what's the normal way to use Box ?? Could someone kindly provide > > > some example on how to use ? I tried to look through some source code > > > of Lift but its not easy for a newbie :) > > > > Thanks -- You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/liftweb?hl=.
