Skip to content

Commit

Permalink
Merge pull request #1228 from lift/dpp_issue_1198
Browse files Browse the repository at this point in the history
Addresses #1198. Made certain Box methods call-by-name
  • Loading branch information
David Pollak committed Mar 1, 2012
2 parents 220a412 + b4c0809 commit a20bb12
Showing 1 changed file with 24 additions and 35 deletions.
59 changes: 24 additions & 35 deletions core/common/src/main/scala/net/liftweb/common/Box.scala
Expand Up @@ -302,12 +302,6 @@ sealed abstract class Box[+A] extends Product {
*/
def isA[B](cls: Class[B]): Box[B] = Empty

/**
* If the partial function is defined at the current Box's value
* apply the partial function.
*/
def collect[B](pf: PartialFunction[A, B]): Box[B]

/**
* Return a Full[B] if the contents of this Box is of type <code>B</code>, otherwise return Empty
*/
Expand Down Expand Up @@ -353,33 +347,33 @@ sealed abstract class Box[+A] extends Product {
* @param msg the failure message
* @return a Failure with the message if this Box is Empty
*/
def ?~(msg: String): Box[A] = this
def ?~(msg: => String): Box[A] = this

/**
* Transform an Empty to a ParamFailure with the specified typesafe
* parameter.
* @param errorCode a value indicating the error
* @return a ParamFailure with the specified value
*/
def ~>[T](errorCode: T): Box[A] = this
def ~>[T](errorCode: => T): Box[A] = this

/**
* Alias for ?~
*/
def failMsg(msg: String): Box[A] = ?~(msg)
def failMsg(msg: => String): Box[A] = ?~(msg)

/**
* Transform an EmptyBox to a Failure with the specified message and chain
* the new Failure to any previous Failure represented by this Box.
* @param msg the failure message
* @return a Failure with the message if this Box is an Empty Box. Chain the messages if it is already a Failure
*/
def ?~!(msg: String): Box[A] = ?~(msg)
def ?~!(msg: => String): Box[A] = ?~(msg)

/**
* Alias for ?~!
*/
def compoundFailMsg(msg: String): Box[A] = ?~!(msg)
def compoundFailMsg(msg: => String): Box[A] = ?~!(msg)

/**
* Filter this box on the specified predicate, returning a Failure with the specified
Expand All @@ -394,7 +388,7 @@ sealed abstract class Box[+A] extends Product {
* This method calls the specified function with the value contained in this Box
* @return the result of the function or a default value
*/
def run[T](in: T)(f: (T, A) => T) = in
def run[T](in: => T)(f: (T, A) => T) = in

/**
* Perform a side effect by passing this Box to the specified function
Expand Down Expand Up @@ -453,6 +447,17 @@ sealed abstract class Box[+A] extends Product {
* Fill with the Box's value
*/
def toLeft[B](right: => B): Either[A, B] = Right(right)


/**
* If the partial function is defined at the current Box's value
* apply the partial function.
*/
final def collect[B](pf: PartialFunction[A, B]): Box[B] = {
flatMap(value =>
if (pf.isDefinedAt(value)) Full(pf(value))
else Empty)
}
}

/**
Expand Down Expand Up @@ -497,7 +502,7 @@ final case class Full[+A](value: A) extends Box[A] {

override def toOption: Option[A] = Some(value)

override def run[T](in: T)(f: (T, A) => T) = f(in, value)
override def run[T](in: => T)(f: (T, A) => T) = f(in, value)

/**
* An <code>Either</code> that is a <code>Left</code> with the given argument
Expand Down Expand Up @@ -532,15 +537,6 @@ final case class Full[+A](value: A) extends Box[A] {
override def ===[B >: A](to: B): Boolean = value == to

override def dmap[B](dflt: => B)(f: A => B): B = f(value)

/**
* If the partial function is defined at the current Box's value
* apply the partial function.
*/
final def collect[B](pf: PartialFunction[A, B]): Box[B] = {
if (pf.isDefinedAt(value)) Full(pf(value))
else Empty
}
}

/**
Expand Down Expand Up @@ -576,19 +572,12 @@ sealed abstract class EmptyBox extends Box[Nothing] {

override def filter(p: Nothing => Boolean): Box[Nothing] = this

override def ?~(msg: String): Failure = Failure(msg, Empty, Empty)
override def ?~(msg: => String): Failure = Failure(msg, Empty, Empty)

override def ?~!(msg: String): Failure = Failure(msg, Empty, Empty)
override def ?~!(msg: => String): Failure = Failure(msg, Empty, Empty)

override def ~>[T](errorCode: T): ParamFailure[T] =
override def ~>[T](errorCode: => T): ParamFailure[T] =
ParamFailure("", Empty, Empty, errorCode)


/**
* If the partial function is defined at the current Box's value
* apply the partial function.
*/
final override def collect[B](pf: PartialFunction[Nothing, B]): Box[B] = this
}

/**
Expand Down Expand Up @@ -674,11 +663,11 @@ sealed case class Failure(msg: String, exception: Box[Throwable], chain: Box[Fai
case _ => false
}

override def ?~(msg: String): Failure = this
override def ?~(msg: => String): Failure = this

override def ?~!(msg: String): Failure = Failure(msg, Empty, Full(this))
override def ?~!(msg: => String): Failure = Failure(msg, Empty, Full(this))

override def ~>[T](errorCode: T): ParamFailure[T] = ParamFailure(msg, exception, chain, errorCode)
override def ~>[T](errorCode: => T): ParamFailure[T] = ParamFailure(msg, exception, chain, errorCode)
}

/**
Expand Down

0 comments on commit a20bb12

Please sign in to comment.