scala.util

class Fluid

[source: scala/util/Fluid.scala]

class Fluid[T](init : T)
extends AnyRef

Fluids provide a binding mechanism where the current value is found through dynamic scope, but where access to the fluid itself is resolved through static binding to a variable referencing the fluid.

The current value can be retrieved with the value method. New values can be pushed using the withValue method. Values pushed via withValue only stay valid while the withValue's second argument, a parameterless closure, executes. When the second argument finishes, the fluid reverts to the previous value.

Usage of withValue looks like this:

  someFluid.withValue(newValue) {
    // ... code called in here that calls value ...
    // ... will be given back the newValue ...
  }
  

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.

Author
Lex Spoon
Version
1.0, 21/03/2006
Method Summary
override def toString : String
def value : T
Retrieve the current value
def value_= (newval : T) : Unit
Change the currently bound value, discarding the old value. Usually withValue() gives better semantics.
def withValue [S](newval : T)(thunk : => S) : S
Set the value of the fluid while executing the specified thunk.
Methods inherited from AnyRef
getClass, hashCode, equals, clone, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Method Details
def value : T
Retrieve the current value

def withValue[S](newval : T)(thunk : => S) : S
Set the value of the fluid while executing the specified thunk.
Parameters
newval - The value to which to set the fluid
thunk - The code to evaluate under the new setting

def value_=(newval : T) : Unit
Change the currently bound value, discarding the old value. Usually withValue() gives better semantics.

override def toString : String