Skip to content
Newer
Older
100644 68 lines (59 sloc) 2.39 KB
22a8618 @lexspoon Renamed Fluid to DynamicVariable
lexspoon authored
1 /* __ *\
2 ** ________ ___ / / ___ Scala API **
807dbe5 @heathermiller Brings all copyrights (in comments) up-to-date, from 2011/12 to 2013
heathermiller authored
3 ** / __/ __// _ | / / / _ | (c) 2006-2013, LAMP/EPFL **
22a8618 @lexspoon Renamed Fluid to DynamicVariable
lexspoon authored
4 ** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5 ** /____/\___/_/ |_/____/_/ | | **
6 ** |/ **
7 \* */
8
9 package scala.util
10
11 import java.lang.InheritableThreadLocal
12
1db8243 @kzys Formatting fixes for scala.util.
kzys authored
13 /** `DynamicVariables` provide a binding mechanism where the current
82770a9 @paulp Introduces "Origins" for all your superhero bac...
paulp authored
14 * value is found through dynamic scope, but where access to the
15 * variable itself is resolved through static scope.
16 *
1db8243 @kzys Formatting fixes for scala.util.
kzys authored
17 * The current value can be retrieved with the value method. New values
18 * should be pushed using the `withValue` method. Values pushed via
19 * `withValue` only stay valid while the `withValue`'s second argument, a
82770a9 @paulp Introduces "Origins" for all your superhero bac...
paulp authored
20 * parameterless closure, executes. When the second argument finishes,
21 * the variable reverts to the previous value.
22 *
23 * {{{
22a8618 @lexspoon Renamed Fluid to DynamicVariable
lexspoon authored
24 * someDynamicVariable.withValue(newValue) {
25 * // ... code called in here that calls value ...
26 * // ... will be given back the newValue ...
27 * }
82770a9 @paulp Introduces "Origins" for all your superhero bac...
paulp authored
28 * }}}
29 *
30 * Each thread gets its own stack of bindings. When a
1db8243 @kzys Formatting fixes for scala.util.
kzys authored
31 * new thread is created, the `DynamicVariable` gets a copy
82770a9 @paulp Introduces "Origins" for all your superhero bac...
paulp authored
32 * of the stack of bindings from the parent thread, and
33 * from then on the bindings for the new thread
34 * are independent of those for the original thread.
22a8618 @lexspoon Renamed Fluid to DynamicVariable
lexspoon authored
35 *
36 * @author Lex Spoon
37 * @version 1.1, 2007-5-21
38 */
39 class DynamicVariable[T](init: T) {
2ed9387 @odersky dual mode (generics or not) version of compiler...
odersky authored
40 private val tl = new InheritableThreadLocal[T] {
41 override def initialValue = init.asInstanceOf[T with AnyRef]
22a8618 @lexspoon Renamed Fluid to DynamicVariable
lexspoon authored
42 }
43
44 /** Retrieve the current value */
45 def value: T = tl.get.asInstanceOf[T]
46
47 /** Set the value of the variable while executing the specified
48 * thunk.
49 *
9e8835d @paulp Fixed #2051.
paulp authored
50 * @param newval The value to which to set the variable
22a8618 @lexspoon Renamed Fluid to DynamicVariable
lexspoon authored
51 * @param thunk The code to evaluate under the new setting
52 */
82770a9 @paulp Introduces "Origins" for all your superhero bac...
paulp authored
53 def withValue[S](newval: T)(thunk: => S): S = {
22a8618 @lexspoon Renamed Fluid to DynamicVariable
lexspoon authored
54 val oldval = value
82770a9 @paulp Introduces "Origins" for all your superhero bac...
paulp authored
55 tl set newval
22a8618 @lexspoon Renamed Fluid to DynamicVariable
lexspoon authored
56
82770a9 @paulp Introduces "Origins" for all your superhero bac...
paulp authored
57 try thunk
58 finally tl set oldval
22a8618 @lexspoon Renamed Fluid to DynamicVariable
lexspoon authored
59 }
60
61 /** Change the currently bound value, discarding the old value.
82770a9 @paulp Introduces "Origins" for all your superhero bac...
paulp authored
62 * Usually withValue() gives better semantics.
22a8618 @lexspoon Renamed Fluid to DynamicVariable
lexspoon authored
63 */
82770a9 @paulp Introduces "Origins" for all your superhero bac...
paulp authored
64 def value_=(newval: T) = tl set newval
22a8618 @lexspoon Renamed Fluid to DynamicVariable
lexspoon authored
65
82770a9 @paulp Introduces "Origins" for all your superhero bac...
paulp authored
66 override def toString: String = "DynamicVariable(" + value + ")"
22a8618 @lexspoon Renamed Fluid to DynamicVariable
lexspoon authored
67 }
Something went wrong with that request. Please try again.