Skip to main content

eval_apply

Function eval_apply 

Source
pub fn eval_apply(func: Value, arg: Value, pos: usize) -> Value
Expand description

func arg - applies a callable or indexable Swan value to an argument.

pos is the source position of the application, used in error values.

Dispatches on the type of func:

  • Value::Builtin(f): calls the native Rust function with the whole arg.
  • Value::Func { ... }: creates a child scope of the closure, binds the parameters to arg (destructuring tuples, see assignment_operation), and evaluates the body there.
  • Value::List: indexing by a zero-based number. A tuple argument gives a tuple of results, normalized (so out-of-range indices are dropped).
  • Value::Namespace: own-only lookup by text key. A tuple argument gives a normalized tuple of results (missing keys are dropped).
  • Value::Text: character indexing by a zero-based number. A tuple argument gives a normalized tuple of results.
  • Anything else (including Nothing and Undefined): returns Undef::BinaryOperation { op: "application", lhv: func, rhv: arg, pos }.

Broadcasting over a tuple of callables is not done here; the callers (ApplicationOp, MappingOp) do it with map_values!.