diff options
author | paltherr <paltherr@epfl.ch> | 2004-11-17 12:44:39 +0000 |
---|---|---|
committer | paltherr <paltherr@epfl.ch> | 2004-11-17 12:44:39 +0000 |
commit | 35c612c5c240555649428fa029ae1c6b9b85ebeb (patch) | |
tree | e6a99f7473a0a983e499ce6aef31f2f8427859e1 | |
parent | 2968ffe5e0441c05a571e46a70d649718eaaeae3 (diff) | |
download | scala-35c612c5c240555649428fa029ae1c6b9b85ebeb.tar.gz scala-35c612c5c240555649428fa029ae1c6b9b85ebeb.tar.bz2 scala-35c612c5c240555649428fa029ae1c6b9b85ebeb.zip |
- Removed ResultOrException
-rw-r--r-- | config/list/library.lst | 1 | ||||
-rw-r--r-- | sources/scala/runtime/ResultOrException.java | 40 | ||||
-rw-r--r-- | sources/scala/runtime/RunTime.java | 12 | ||||
-rw-r--r-- | sources/scala/runtime/ScalaRunTime.scala | 42 |
4 files changed, 39 insertions, 56 deletions
diff --git a/config/list/library.lst b/config/list/library.lst index 3db13230c5..66f0014cc1 100644 --- a/config/list/library.lst +++ b/config/list/library.lst @@ -153,7 +153,6 @@ runtime/FNV_Hash.java runtime/PearsonHash.java runtime/IOMap.java runtime/InterpreterSupport.java -runtime/ResultOrException.java runtime/RunTime.java runtime/ScalaRunTime.scala diff --git a/sources/scala/runtime/ResultOrException.java b/sources/scala/runtime/ResultOrException.java deleted file mode 100644 index 4961c785d7..0000000000 --- a/sources/scala/runtime/ResultOrException.java +++ /dev/null @@ -1,40 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2002, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - -package scala.runtime; - -/** @meta class [?A] extends java.lang.Object; - */ -public class ResultOrException { - - /** @meta field ?A; - */ - public Object result; - - public Throwable exc; - - /** @meta constr(?A, java.lang.Throwable); - */ - ResultOrException(Object result, Throwable exc) { - this.result = result; - this.exc = exc; - } - - /** @meta method [?A] (def ?A) scala.runtime.ResultOrException[?A]; - */ - public static ResultOrException tryBlock(scala.Function0 block) { - try { - return new ResultOrException(block.apply(), null); - } catch (Throwable ex) { - return new ResultOrException(null, ex); - } - } -} - diff --git a/sources/scala/runtime/RunTime.java b/sources/scala/runtime/RunTime.java index fbc08e716e..b5b8e98fb7 100644 --- a/sources/scala/runtime/RunTime.java +++ b/sources/scala/runtime/RunTime.java @@ -72,6 +72,18 @@ public abstract class RunTime { } //######################################################################## + // Public Functions - Catching exceptions + + public static Throwable tryCatch(Runnable runnable) { + try { + runnable.run(); + return null; + } catch (Throwable exception) { + return exception; + } + } + + //######################################################################## // Public Functions - Boxing primitives public static Unit box_uvalue( ) { diff --git a/sources/scala/runtime/ScalaRunTime.scala b/sources/scala/runtime/ScalaRunTime.scala index 7a13eac8c9..b2920999e6 100644 --- a/sources/scala/runtime/ScalaRunTime.scala +++ b/sources/scala/runtime/ScalaRunTime.scala @@ -12,20 +12,32 @@ package scala.runtime; object ScalaRunTime { - class Try[a](r: scala.runtime.ResultOrException[a]) { - def Catch[b >: a](handler: PartialFunction[Throwable, b]): b = - if (r.exc == null) - r.result.asInstanceOf[b] - else if (/*!(r.exc is NonLocalReturn) && */handler isDefinedAt r.exc) - handler(r.exc) - else - throw r.exc; - - def Finally(handler: Unit): a = - if (r.exc == null) r.result.asInstanceOf[a] else throw r.exc; - } - - def Try[a](def/*!!!*/ block: a): Try[a] = - new Try(ResultOrException.tryBlock(block)); + trait Try[a] { + def Catch[b >: a](handler: PartialFunction[Throwable, b]): b; + def Finally(handler: Unit): a; + } + + def Try[a](block: => a): Try[a] = new Try[a] with Runnable { + var result: a = _; + var exception: Throwable = RunTime.tryCatch(this); + + def run(): Unit = result = block; + + def Catch[b >: a](handler: PartialFunction[Throwable, b]): b = + if (exception == null) + result.asInstanceOf[b] + // !!! else if (exception is LocalReturn) + // !!! // ... + else if (handler isDefinedAt exception) + handler(exception) + else + throw exception; + + def Finally(handler: Unit): a = + if (exception == null) + result.asInstanceOf[a] + else + throw exception; + } } |