Box ha a number of benefits over null -- type system enforced null checks, stringing computations that can fail together in a safe fashion, error handling, and so on.
Usually you only need to handle the case where there is a value and map, foreach or for comprehensions are good for that. If you want to branch and handle both has-a-value (Full) and doesn't (Empty or Failure) try using pattern matching: S.cookie("foobar") match { case Full(cookie) => // do something with cookie case _ => // do something without cookie } You can also combine pattern matching, comprehensions, and so on for more complicated things (e.g. operate on multiple boxes, etc.) HTH, -Ross On Nov 16, 2009, at 5:08 AM, DMB <combust...@gmail.com> wrote: > > Except if there's no cookie, in almost all cases I'll likely want to > branch and do something else (write out a cookie, for instance), and > the non-evaluation of stuff doesn't help me much with that. > > And there is checking, and memory allocation / deallocation hidden in > the for comprehension and Box anyway. It's neither expensive, nor > verbose, so I don't see null checking as a big deal. > > On Nov 16, 1:45 am, Sergey Andreev <andser...@gmail.com> wrote: >> To avoid null checking. If there is no cookie, you will get Empty >> otherwise >> Full(cookie). Moreover with for-comprehensions you don't need any >> checks at >> all. If there is no cookie then the doSomethingWithValue won't be >> evaluated. >> >> On Mon, Nov 16, 2009 at 12:34 PM, DMB <combust...@gmail.com> wrote: >> >>> I guess that could work, but why go to such lengths where there are >>> much more straightforward solutions available? What do for >>> comprehensions buy you in this case? I mean, 99% of the time, when I >>> want to check for a cookie, I don't need the cookie itself or any of >>> its properties. I need its value, or null if there's no cookie. Why >>> not do something a-la RoR: >> >>> S.cookieValue("cookieName") >> >>> or a-la ASP.NET: >> >>> val c = S.cookie("cookieName") >>> if(c != null) { >>> val v = c.value >>> } >> >>> Or, indeed, both? >> >>> On Nov 16, 1:22 am, Sergey Andreev <andser...@gmail.com> wrote: >>>> Hi, >> >>>> For-comprehensions could help you out: >> >>>> for{ >>>> cookie <- S.findCookie(cookieName) >>>> value <- cookie} doSomethingWithValue >> >>>> Regards >> >>>> On Mon, Nov 16, 2009 at 12:07 PM, DMB <combust...@gmail.com> wrote: >> >>>>> When I call findCookie it returns a Box. Then, the value on the >>>>> cookie >>>>> itself is also a box. Hence a ruby one-liner turns into something >>>>> like: >> >>>>> val cookie = S.findCookie(cookieName) >>>>> if(cookie.isDefined) { >>>>> val cookieVal = cookie.open_!.value.openOr(null) >>>>> // Do something with the cookie value >>>>> } >> >>>>> This is very ugly, so I'm guessing I'm doing something wrong, >>>>> but try >>>>> as I might, I could not find any examples that would look even >>>>> vaguely >>>>> "right" to me. >> >>>>> Why can't findCookie return a simple, unboxed HTTPCookie object or >>>>> null if cookie is not found? >>>>> Why does the value inside a cookie need to also be Box'ed? >> >>>>> For the sake of comparison, here's how you do the same thing in >>>>> RoR: >>>>> v = cookies["cookieName"] >>>>> // Do something with the cookie >> >>>>> or ASP.NET: >>>>> var c = Request.Cookies["CookieName"] >>>>> if(c != null) { >>>>> var v = c.Value >>>>> // Do something with the cookie >>>>> } >> >>>>> I fail to see why Lift should be more complicated. >> >>>>> This is with Lift 1.1 M7 > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---