pub enum Closure {
One(String, Procedure),
Two(String, String, Procedure),
Primary(Box<Primary>),
}Expand description
Represents a closure (anonymous function) expression in the AIMX language.
Closures capture their environment and can be used as first-class values.
They are typically created using the => operator and can have one or two parameters.
§Variants
One(String, Procedure): A closure with a single parameterTwo(String, String, Procedure): A closure with two parametersPrimary(Box<Primary>): An optimized representation when the closure is a simple primary expression
§Examples
// Parses to Closure::One("x", procedure)
x => x + 1
// Parses to Closure::Two("x", "y", procedure)
(x, y) => x + yVariants§
One(String, Procedure)
Two(String, String, Procedure)
Primary(Box<Primary>)
Primary flattened AST optimization - used when the closure body is a simple primary expression
Implementations§
Source§impl Closure
impl Closure
Sourcepub fn invoke(&self, context: &mut dyn ContextLike) -> Result<Value>
pub fn invoke(&self, context: &mut dyn ContextLike) -> Result<Value>
Invokes the closure with the current context.
This method evaluates the closure body after setting up the parameter bindings in the context. For closures with parameters, the parameters are bound to special context keys (0 for single parameter, 0 and 1 for double parameters).
§Arguments
context- The evaluation context
§Returns
Returns Result<Value> containing the result of evaluating the closure body.
§Examples
let mut context = Context::new();
let closure = Closure::One("x".to_string(), Procedure::Primary(Box::new(Primary::Literal(aimx::Literal::Number(42.0)))));
// Parameter values are set using the context's key mechanism
let param_name = "x".to_string();
context.set_key(0, ¶m_name);
let result = closure.invoke(&mut context)?;
// result should be Value::Literal(Literal::Number(42.0))Trait Implementations§
Source§impl ExpressionLike for Closure
impl ExpressionLike for Closure
Source§fn evaluate(&self, context: &mut dyn ContextLike) -> Result<Value>
fn evaluate(&self, context: &mut dyn ContextLike) -> Result<Value>
Evaluates the closure expression.
For primary closures (optimized simple expressions), this evaluates the expression directly.
For parameterized closures, this returns a Value::Closure containing the closure itself,
since closures with parameters need to be invoked with invoke() to evaluate them.
§Arguments
context- The evaluation context
§Returns
Returns Result<Value> containing either the evaluated result (for primary closures)
or the closure itself as a value (for parameterized closures).
§Examples
let mut context = Context::new();
let closure = Closure::Primary(Box::new(Primary::Literal(aimx::Literal::Number(42.0))));
let result = closure.evaluate(&mut context)?;
// result should be Value::Literal(Literal::Number(42.0))Source§fn write(&self, writer: &mut Writer)
fn write(&self, writer: &mut Writer)
Writes the closure expression to a writer.
This method formats the closure according to its variant and writes it to the provided writer. The output matches the AIMX syntax for closures.
§Arguments
writer- The writer to write the formatted closure to
Source§fn to_sanitized(&self) -> String
fn to_sanitized(&self) -> String
Returns a sanitized string representation of the closure.
Sanitized output is suitable for display to users and may include formatting that makes the expression easier to read.
§Returns
Returns a String containing the sanitized closure representation.
Source§fn to_formula(&self) -> String
fn to_formula(&self) -> String
Returns a formula string representation of the closure.
Formula output is suitable for use as input to the parser and matches the exact AIMX syntax.
§Returns
Returns a String containing the formula representation.
impl StructuralPartialEq for Closure
Auto Trait Implementations§
impl Freeze for Closure
impl RefUnwindSafe for Closure
impl Send for Closure
impl Sync for Closure
impl Unpin for Closure
impl UnwindSafe for Closure
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.