2009-02-16 19:50:50 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009 Scalable Solutions.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-03-23 19:17:49 +01:00
|
|
|
package se.scalablesolutions.akka.kernel
|
2009-02-16 19:50:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reference that can hold either a typed value or an exception.
|
|
|
|
|
*
|
|
|
|
|
* Usage:
|
|
|
|
|
* <pre>
|
|
|
|
|
* scala> ErrRef(1)
|
|
|
|
|
* res0: ErrRef[Int] = ErrRef@a96606
|
|
|
|
|
*
|
|
|
|
|
* scala> res0()
|
|
|
|
|
* res1: Int = 1
|
|
|
|
|
*
|
|
|
|
|
* scala> res0() = 3
|
|
|
|
|
*
|
|
|
|
|
* scala> res0()
|
|
|
|
|
* res3: Int = 3
|
|
|
|
|
*
|
|
|
|
|
* scala> res0() = { println("Hello world"); 3}
|
|
|
|
|
* Hello world
|
|
|
|
|
*
|
|
|
|
|
* scala> res0()
|
|
|
|
|
* res5: Int = 3
|
|
|
|
|
*
|
|
|
|
|
* scala> res0() = error("Lets see what happens here...")
|
|
|
|
|
*
|
|
|
|
|
* scala> res0()
|
|
|
|
|
* java.lang.RuntimeException: Lets see what happens here...
|
|
|
|
|
* at ErrRef.apply(RefExcept.scala:11)
|
|
|
|
|
* at .<init>(<console>:6)
|
|
|
|
|
* at .<clinit>(<console>)
|
|
|
|
|
* at Re...
|
|
|
|
|
* </pre>
|
2009-03-23 19:17:49 +01:00
|
|
|
*
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
2009-02-16 19:50:50 +01:00
|
|
|
*/
|
2009-03-23 19:17:49 +01:00
|
|
|
class ErrRef[Payload](payload: Payload, val tx: Option[Transaction]){
|
|
|
|
|
private[this] var contents: Either[Throwable, Payload] = Right(payload)
|
|
|
|
|
|
|
|
|
|
def update(value: => Payload) = {
|
|
|
|
|
contents = try { Right(value) } catch { case (e : Throwable) => Left(e) }
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-16 19:50:50 +01:00
|
|
|
def apply() = contents match {
|
2009-03-23 19:17:49 +01:00
|
|
|
case Right(payload) => payload
|
2009-02-16 19:50:50 +01:00
|
|
|
case Left(e) => throw e.fillInStackTrace
|
|
|
|
|
}
|
2009-03-23 19:17:49 +01:00
|
|
|
|
2009-02-16 19:50:50 +01:00
|
|
|
override def toString(): String = "ErrRef[" + contents + "]"
|
|
|
|
|
}
|
|
|
|
|
object ErrRef {
|
2009-03-23 19:17:49 +01:00
|
|
|
def apply[Payload](payload: Payload, tx: Option[Transaction]) = new ErrRef(payload, tx)
|
2009-02-16 19:50:50 +01:00
|
|
|
}
|