From 8cf7228f8c1e11d67f4b7053b4ff2772e2bd79fc Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Sat, 20 Jun 2009 16:55:48 +0000 Subject: Created DelayedLazyVal for your def/val hybrid ... Created DelayedLazyVal for your def/val hybrid needs. --- .../scala/tools/nsc/interpreter/Completion.scala | 16 +------- .../scala/concurrent/DelayedLazyVal.scala | 1 + src/library/scala/concurrent/DelayedLazyVal.scala | 44 ++++++++++++++++++++++ 3 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 src/dotnet-library/scala/concurrent/DelayedLazyVal.scala create mode 100644 src/library/scala/concurrent/DelayedLazyVal.scala diff --git a/src/compiler/scala/tools/nsc/interpreter/Completion.scala b/src/compiler/scala/tools/nsc/interpreter/Completion.scala index 2239f7cc3a..599ad8fa18 100644 --- a/src/compiler/scala/tools/nsc/interpreter/Completion.scala +++ b/src/compiler/scala/tools/nsc/interpreter/Completion.scala @@ -24,6 +24,7 @@ package scala.tools.nsc.interpreter import jline._ import java.net.URL import java.util.concurrent.ConcurrentHashMap +import scala.concurrent.DelayedLazyVal // REPL completor - queries supplied interpreter for valid completions // based on current contents of buffer. @@ -32,23 +33,10 @@ class Completion(val interpreter: Interpreter) extends Completor { import java.util.{ List => JList } import interpreter.compilerClasspath - // This is a wrapper which lets us use a def until some lengthy - // action is complete, and then a val from that point on. - class LazyValFuture[T](f: () => T, body: => Unit) { - private[this] var isDone = false - private[this] lazy val complete = f() - def apply(): T = if (isDone) complete else f() - - scala.concurrent.ops.future { - body - isDone = true - } - } - // it takes a little while to look through the jars so we use a future and a concurrent map class CompletionAgent { val dottedPaths = new ConcurrentHashMap[String, List[String]] - val topLevelPackages = new LazyValFuture( + val topLevelPackages = new DelayedLazyVal( () => enumToList(dottedPaths.keys) filterNot (_ contains '.'), getDottedPaths(dottedPaths, interpreter) ) diff --git a/src/dotnet-library/scala/concurrent/DelayedLazyVal.scala b/src/dotnet-library/scala/concurrent/DelayedLazyVal.scala new file mode 100644 index 0000000000..66ec24f8eb --- /dev/null +++ b/src/dotnet-library/scala/concurrent/DelayedLazyVal.scala @@ -0,0 +1 @@ +/* DelayedLazyVal does not exist for the dotnet target */ diff --git a/src/library/scala/concurrent/DelayedLazyVal.scala b/src/library/scala/concurrent/DelayedLazyVal.scala new file mode 100644 index 0000000000..0fa3c1660b --- /dev/null +++ b/src/library/scala/concurrent/DelayedLazyVal.scala @@ -0,0 +1,44 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + +package scala.concurrent + +import annotation.experimental + +/** A DelayedLazyVal is a wrapper for lengthy + * computations which have a valid partially computed result. + * The first argument is a function for obtaining the result + * at any given point in time, and the second is the lengthy + * computation. Once the computation is complete, the apply() + * method will stop recalculating it and return a fixed value + * from that point forward. + * + * @param f the function to obtain the current value at any point in time + * @param body the computation to run to completion in another thread + * + * @author Paul Phillips + * @version 2.8 + */ +@experimental +class DelayedLazyVal[T](f: () => T, body: => Unit) { + @volatile private[this] var isDone = false + private[this] lazy val complete = f() + + /** The current result of f(), or the final result if complete. + * + * @return the current value + */ + def apply(): T = if (isDone) complete else f() + + ops.future { + body + isDone = true + } +} \ No newline at end of file -- cgit v1.2.3