fork method Null safety

SymbolTable<T> fork(
  1. {Map<String, T> values = const {}}
)

Creates a forked scope, derived from this one. You may provide starter values.

As opposed to createChild, all variables in the resulting forked scope will be copies of those in this class. This makes forked scopes useful for implementations of concepts like closure functions, where the current values of variables are trapped.

The forked scope is essentially orphaned and stands alone; although its parent getter will point to the parent of the original scope, the parent will not be aware of the new scope's existence.

Implementation

SymbolTable<T> fork({Map<String, T> values = const {}}) {
  var table = SymbolTable<T>();

  table
    .._depth = _depth
    .._parent = _parent
    .._root = _root;

  table._variables.addAll(_variables.map((Variable v) {
    var variable = Variable<T>._(v.name, this, value: v.value as T?);
    variable.visibility = v.visibility;

    if (v.isImmutable) variable.lock();
    return variable;
  }));

  return table;
}