Class Scoped<T>

java.lang.Object
gov.nasa.jpl.aerie.merlin.framework.Scoped<T>
All Implemented Interfaces:
Supplier<T>

public final class Scoped<T> extends Object implements Supplier<T>
A dynamically-scoped cell. Values pushed into the cell are strictly popped in the same order, and the lifetime of each value is tied to the call stack. See the related concept of scoped variables in Project Loom.
  • Method Details

    • create

      public static <T> Scoped<T> create()
      Create a dynamically-scoped cell containing values of type Scoped.
      Type Parameters:
      T - The type of value this cell will contain.
      Returns:
      A new cell that can contain values of type Scoped.
    • get

      public T get() throws Scoped.EmptyDynamicCellException
      Gets the current value of the dynamic cell. Throws an exception if nobody up-stack is currently serving this cell.
      Specified by:
      get in interface Supplier<T>
      Throws:
      Scoped.EmptyDynamicCellException
    • set

      public Scoped.UndoToken<T> set(T newValue)
      Set a value in this cell, returning an AutoCloseable resource restoring the previous value on close.

      This method should always be used in a try-with-resources statement:

       try (final var scope = cell.set(value)) {
         // ...
       }