Packages

trait Var[+T] extends AnyRef

Vars are values that vary over time. To create one, you must give it an initial value.

val a = Var[Int](1)

A Var created this way can be sampled to retrieve its current value,

println(Var.sample(a)) // prints 1

or, invoked to assign it new values.

a.update(2)
println(Var.sample(a)) // prints 2

Vars can be derived from other Vars.

val b = a.flatMap { x => Var(x + 2) }
println(Var.sample(b)) // prints 4

And, if the underlying is assigned a new value, the derived Var is updated. Updates are computed lazily, so while assignment is cheap, sampling is where we pay the cost of the computation required to derive the new Var.

a.update(1)
println(Var.sample(b)) // prints 3

A key difference between the derived Var and its underlying is that derived Vars can't be assigned new values. That's why b, from the example above, can't be invoked to assign it a new value, it can only be sampled.

Self Type
Var[T]
Note

Vars do not always perform the minimum amount of re-computation.

,

There are no well-defined error semantics for Var. Vars are computed lazily, and the updating thread will receive any exceptions thrown while computing derived Vars. Note: There is a Java-friendly API for this trait: com.twitter.util.AbstractVar.

Linear Supertypes
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Var
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Abstract Value Members

  1. abstract def observe(depth: Int, obs: Observer[T]): Closable

    Concrete implementations of Var implement observe.

    Concrete implementations of Var implement observe. This is called for each toplevel observe. Depths indicate the relative structural depth of the observation, from the frame of reference of the root call to observe. (Each Var derived via flatMap increases the depth.) Depths are used to order the invocation of update callbacks. This is used to ensure that updates proceed in topological order so that every input variable is fully resolved before recomputing a derived variable.

    Attributes
    protected

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. lazy val changes: Event[T]

    An Event where changes in Var are emitted.

    An Event where changes in Var are emitted. The current value of this Var is emitted synchronously upon subscription.

    All changes to this Var are guaranteed to be published to the Event.

  6. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
  7. def diff[CC[_], U](implicit arg0: Diffable[CC], toCC: <:<[T, CC[U]]): Event[Diff[CC, U]]

    Produce an Event reflecting the differences between each update to this Var.

  8. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  9. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
  11. def flatMap[U](f: (T) => Var[U]): Var[U]

    Create a dependent Var which behaves as f applied to the current value of this Var.

    Create a dependent Var which behaves as f applied to the current value of this Var. FlatMap manages a dynamic dependency graph: the dependent Var is detached and recomputed whenever the outer Var changes, but only if there are any observers. An unobserved Var returned by flatMap will not invoke f

  12. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  13. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  14. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  15. def join[U](other: Var[U]): Var[(T, U)]
  16. def map[U](f: (T) => U): Var[U]

    Create a derived variable by applying f to the contained value.

  17. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  18. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  19. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  20. def sample(): T
  21. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  22. def toString(): String
    Definition Classes
    AnyRef → Any
  23. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  24. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  25. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()

Inherited from AnyRef

Inherited from Any

Ungrouped