pub struct Scope(pub Rc<RefCell<ScopeData>>);Expand description
A lexical environment: a table of bindings with an optional parent.
Name lookup (Scope::get) searches the scope itself and then its
ancestors. New bindings (Scope::set) always go into the scope itself.
A Scope is also the storage of a Value::Namespace.
Scope is a cheap handle: clones share the same bindings.
§Examples
use swan_core::value::{Scope, Value};
let parent = Scope::new();
parent.set("x", Value::Numb(1.0));
let child = parent.new_child();
child.set("y", Value::Numb(2.0));
assert_eq!(child.get("x"), Value::Numb(1.0)); // inherited
assert_eq!(parent.get("y"), Value::Nothing); // not visible upwardsTuple Fields§
§0: Rc<RefCell<ScopeData>>Implementations§
Source§impl Scope
impl Scope
Sourcepub fn with_parent(parent: Scope) -> Self
pub fn with_parent(parent: Scope) -> Self
Create a child scope that inherits from a parent.
Sourcepub fn new_child(&self) -> Self
pub fn new_child(&self) -> Self
Creates a new child scope using self as the parent environment.
Sourcepub fn get(&self, key: &str) -> Value
pub fn get(&self, key: &str) -> Value
Returns the value bound to key in this scope or its nearest ancestor
that binds it, or Value::Nothing if no scope binds it.
The borrow on self is released before recursing into the parent so
that there is no live RefCell borrow held across the recursive call.
This prevents a runtime panic in the (unlikely but possible) event that
the same Scope node appears more than once in the chain.
Sourcepub fn lift(&self, key: &str) -> Value
pub fn lift(&self, key: &str) -> Value
Returns the value bound to key in the outermost scope of the chain
that binds it, or Value::Nothing if no scope binds it.
This is the opposite search order of Scope::get. It implements the
Swan lifting operator $name.