remove method Null safety

Variable<T>? remove(
  1. String name
)

Removes the variable with the given name from this scope, or an ancestor.

Returns the deleted variable, or null.

Note: This may cause resolve calls in forked scopes to return null. Note: There is a difference between symbol tables created via fork, createdChild, and clone.

Implementation

Variable<T>? remove(String name) {
  SymbolTable<T>? search = this;

  while (search != null) {
    var variable = search._variables.firstWhereOrNull((v) => v.name == name);

    if (variable != null) {
      search._wipeLookupCache(name);
      search._variables.remove(variable);
      return variable;
    }

    search = search._parent;
  }

  return null;
}