The short answer is that sometimes inheritance is better than a flat ADT.

A Box can be full or empty: Full or EmptyBox

An empty box can contain no additional information as to why it's empty (the
Empty singleton) or it can contain more information as to why the Box is
empty: Failure.  Now, a Failure could have subclasses such as Failure with
exception and chained Failure, but it seemed to me that there would be an
explosion of subclasses, so I just chose Failure with the various
parameters.

Then someone pointed out that it'd be nice to carry around an HTTP response
code or other data about the Failure which led to ParamFailure.  Because
ParamFailure is a subclass of Failure, you can pattern match on Failure and
it will catch ParamFailure, but you lose the information about the parameter
(just like you can match against EmptyBox if you don't care why the Box is
empty)

The code:

  implicit def handleFailure[T](value: Box[T])(implicit cvt: T =>
LiftResponse): Box[LiftResponse] = {
    value match {
      case ParamFailure(msg, _, _, code: Int) =>
        Full(InMemoryResponse(msg.getBytes("UTF-8"), ("Content-Type" ->
"text/plain; charset=utf-8") :: Nil, Nil, code))

      case Failure(msg, _, _) => Full(NotFoundResponse(msg))

      case Empty => Empty

      case Full(x) => Full(cvt(x))
    }
  }

Gives you a good indication of how various subclasses of Box can work
together to give you a nice way to write REST calls.


On Wed, Mar 3, 2010 at 3:42 PM, Francois <[email protected]> wrote:

> Hello,
>
> I really like the use of Box (perhaps one of the best thing in lift :),
> safe for how it handles ParamFailure - so I'm wondering why ParamFailure is
> a subclass of Failure, and not a full case of Box ? Or why Failure does not
> have (always) an option param, like its option exception/chain ?
>
>
>
> Thanks,
>
> PS: if the answer was already given, I'm sorry, I didn't find it in ml
> archive
>
> --
> Francois ARMAND
> http://fanf42.blogspot.com
>
> --
> 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]<liftweb%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/liftweb?hl=en.
>
>


-- 
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

-- 
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.

Reply via email to