From 35c612c5c240555649428fa029ae1c6b9b85ebeb Mon Sep 17 00:00:00 2001 From: paltherr Date: Wed, 17 Nov 2004 12:44:39 +0000 Subject: - Removed ResultOrException --- sources/scala/runtime/ResultOrException.java | 40 -------------------------- sources/scala/runtime/RunTime.java | 12 ++++++++ sources/scala/runtime/ScalaRunTime.scala | 42 ++++++++++++++++++---------- 3 files changed, 39 insertions(+), 55 deletions(-) delete mode 100644 sources/scala/runtime/ResultOrException.java (limited to 'sources') 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 @@ -71,6 +71,18 @@ public abstract class RunTime { RunTime.loader = loader; } + //######################################################################## + // Public Functions - Catching exceptions + + public static Throwable tryCatch(Runnable runnable) { + try { + runnable.run(); + return null; + } catch (Throwable exception) { + return exception; + } + } + //######################################################################## // Public Functions - Boxing primitives 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; + } } -- cgit v1.2.3