blob: 552e9462508e94482ec73121cbe6870bdec0f2c9 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2006-2008, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id$
package scala.util
import Predef._
import java.lang.InheritableThreadLocal
/** <p>
* DynamicVariables provide a binding mechanism where the current
* value is found through <em>dynamic scope</em>, but where
* access to the variable itself is resolved through <em>static
* scope</em>.
* </p>
* <p>
* The current value can be retrieved with the
* <code>value</code> method. New values should be
* pushed using the <code>withValue</code> method.
* Values pushed via <code>withValue</code> only
* stay valid while the <code>withValue</code>'s
* <em>second</em> argument, a parameterless closure,
* executes. When the second argument finishes,
* the variable reverts to the previous value.
* </p>
* <p>
* Usage of <code>withValue</code> looks like this:
* </p>
* <blockquote><pre>
* someDynamicVariable.withValue(newValue) {
* // ... code called in here that calls value ...
* // ... will be given back the newValue ...
* }
* </pre></blockquote>
* <p>
* Each thread gets its own stack of bindings. When a
* new thread is created, the fluid gets a copy of
* the stack of bindings from the parent thread, and
* from then on the bindings for the new thread
* are independent of those for the original thread.
* </p>
*
* @author Lex Spoon
* @version 1.1, 2007-5-21
*/
class DynamicVariable[T](init: T) {
private val tl = new InheritableThreadLocal[T] {
override def initialValue = init.asInstanceOf[T with AnyRef]
}
/** Retrieve the current value */
def value: T = tl.get.asInstanceOf[T]
/** Set the value of the variable while executing the specified
* thunk.
*
* @param newval The value to which to set the fluid
* @param thunk The code to evaluate under the new setting
*/
def withValue[S](newval: T)(thunk: =>S): S = {
val oldval = value
tl.set(newval)
try { thunk } finally {
tl.set(oldval)
}
}
/** Change the currently bound value, discarding the old value.
* Usually <code>withValue()</code> gives better semantics.
*/
def value_=(newval: T) = { tl.set(newval) }
override def toString: String = "DynamicVariable(" + value +")"
}
|