The use of open_! seems safe to me here because all paths return Full(String).
I never thought of .map(), it was always a list operation for me. But the shortness of the expression is impressive. Still have to get used to the for comprehension, so the .map() approach will be the new favorite. :) Thanks a lot! Eros Jean-Luc wrote: > *open_!* can be very convenient, but it can throw unexpected exceptions. > Better to avoid its use whenever possible. > > And what about something like that : > > val username = User.find(id).map(_.name) getOrElse "User not found" > > or > > val username = (for(user <- User.find(id)) yield user.name > <http://user.name>) getOrElse "User not found" > > Jean-Luc > > > 2009/11/16 Eros Candelaresi <[email protected] > <mailto:[email protected]>> > > > Hi again, > > sorry for answering my own post. Should have looked at the > documentation > first. > > It's as simple as: > > val username = User.find(id).choice( Full(_.name) )( Full("User not > found") ).open_! > (not checked for syntax!) > > Regards, > Eros > > > Eros Candelaresi wrote: > > Hi, > > > > I like the getOrElse way of accessing data very much as it allows to > > handle non-existing values with very little code. However, I am > still > > looking for a more elegant replacement for this: > > > > val username = User.find(id).getOrElse(User.create.name > <http://User.create.name>("User not > > found")).name > > > > Ok, this is very short already. But still it feels wrong to > create an > > instance of User (AFAIK a rather expensive operation, especially > when > > used while creating lists) when all I need is a string. > > > > How do you guys handle such cases? > > > > Regards, > > Eros > > > > > > David Pollak wrote: > > > >> On Thu, Nov 12, 2009 at 5:53 AM, Ferdinand Chan > <[email protected] <mailto:[email protected]> > >> <mailto:[email protected] <mailto:[email protected]>>> wrote: > >> > >> > >> David, > >> > >> Thanks for the blog entry and it does a great job in > explaining how > >> Option works and how to use it. > >> > >> May I suggest that we add a link to this blog post on Lift > Wiki??? > >> Correct me if I'm wrong but I think most newbie to Scala / > Lift may > >> wonder and puzzle how Box / Option works. > >> > >> I know you're very busy and with your permission, I'm > willing to add > >> this to the wiki > >> > >> > >> No permission required. Please add whatever valuable things to the > >> Lift wiki (the one at http://wiki.github.com/dpp/liftweb ) > >> > >> > >> > >> Oh, and I think I really need to tweak my mind a bit and > start to > >> think in a more functional / scala way > >> > >> > >> Yeah... it's a change in mindset, but I think you'll find the > change > >> to be welcome in terms of your own productivity, your ability > to read > >> others' code, and overall code stability. > >> > >> Thanks, > >> > >> David > >> > >> > >> > >> Cheers, > >> > >> Ferdinand > >> > >> On Nov 12, 6:55 am, David Pollak > <[email protected] <mailto:[email protected]> > >> <mailto:[email protected] > <mailto:[email protected]>>> > >> wrote: > >> > > >> > http://blog.lostlake.org/index.php?/archives/50-The-Scala-Option-clas... > >> > > >> > The for comprehension is your friend. > >> > > >> > > >> > > >> > > >> > > >> > On Wed, Nov 11, 2009 at 2:40 PM, Ferdinand Chan > >> <[email protected] <mailto:[email protected]> > <mailto:[email protected] <mailto:[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 > >> > > >> > -- > >> > Lift, the simply functional web > frameworkhttp://liftweb.net <http://liftweb.net> > >> <http://liftweb.net> > >> > Beginning Scalahttp://www.apress.com/book/view/1430219890 > <http://www.apress.com/book/view/1430219890> > >> <http://www.apress.com/book/view/1430219890> > >> > Follow me:http://twitter.com/dpp > >> > Surf the harmonics > >> > >> > >> > >> > >> -- > >> Lift, the simply functional web framework http://liftweb.net > >> Beginning Scala http://www.apress.com/book/view/1430219890 > >> Follow me: http://twitter.com/dpp > >> Surf the harmonics > >> > >> > > > > > > > > > > > > > > > > -- > Jean-Luc Canela > [email protected] <mailto:[email protected]> > > > --~--~---------~--~----~------------~-------~--~----~ 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=en -~----------~----~----~----~------~----~------~--~---
