Class: Heist::Runtime::Binding
- Inherits:
-
Object
- Object
- Heist::Runtime::Binding
- Includes:
- Expression
- Defined in:
- lib/runtime/binding.rb
Overview
A Binding
is a simple data object that couples an Expression
to a Scope
so the expression can be evaluated at some later time. Evaluation is typically memoized, though this is not always the case. Binding
s are analogous to the notion of ‘promises’ in Scheme, though in Heist promises are implemented (as suggested in R5RS) using a (delay)
macro that produces memoized closures. Binding
s are used to implement lazy evaluation, and to maintain lexical scope in hygienic macro expansions by tying variables in the expansion to the lexical scope of the macro.
The Binding
class mixes in the Expression
module purely as a flag to indicate to the evaluator that it can be evaluated.
Instance Attribute Summary collapse
-
#expression ⇒ Object
readonly
Returns the value of attribute expression.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Attributes included from Expression
Instance Method Summary collapse
-
#eval(scope) ⇒ Object
This method is provided as a convenience so that a
Binding
may be treated like any other expression during evaluation. -
#force! ⇒ Object
Evaluates the
Binding
and returns the result. -
#initialize(expression, scope, memoized = true) ⇒ Binding
constructor
To initialize a
Binding
supply anExpression
and aScope
in which to evaluate it. -
#to_s ⇒ Object
(also: #inspect)
Returns a string representation of the binding’s
Expression
.
Methods included from Expression
Constructor Details
#initialize(expression, scope, memoized = true) ⇒ Binding
To initialize a Binding
supply an Expression
and a Scope
in which to evaluate it. An optional third argument states whether the evaluation should be memoized.
25 26 27 28 29 |
# File 'lib/runtime/binding.rb', line 25 def initialize(expression, scope, memoized = true) @expression = expression @scope = scope @memoized = !!memoized end |
Instance Attribute Details
#expression ⇒ Object (readonly)
Returns the value of attribute expression.
20 21 22 |
# File 'lib/runtime/binding.rb', line 20 def expression @expression end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
20 21 22 |
# File 'lib/runtime/binding.rb', line 20 def scope @scope end |
Instance Method Details
#eval(scope) ⇒ Object
This method is provided as a convenience so that a Binding
may be treated like any other expression during evaluation. All it does is return the result of calling force!
.
41 42 43 |
# File 'lib/runtime/binding.rb', line 41 def eval(scope) force! end |
#force! ⇒ Object
Evaluates the Binding
and returns the result. The Expression
is only ever evaluated once if the Binding
has been memoized.
33 34 35 36 |
# File 'lib/runtime/binding.rb', line 33 def force! return @value if defined?(@value) and @memoized @value = Heist.evaluate(@expression, @scope) end |
#to_s ⇒ Object Also known as: inspect
Returns a string representation of the binding’s Expression
.
46 47 48 |
# File 'lib/runtime/binding.rb', line 46 def to_s @expression.to_s end |