Skip to main content

calculate_captures

Function calculate_captures 

Source
pub fn calculate_captures(
    body_effect: &Effect,
    call_effect: &Effect,
) -> Result<Vec<Type>, String>
Expand description

Calculate capture types for a closure

Given:

  • body_effect: what the quotation body needs (e.g., Int Int -- Int)
  • call_effect: what the call site will provide (e.g., Int -- Int)

Returns:

  • captures: types to capture from creation stack (e.g., [Int])

§Capture Ordering

Captures are returned bottom-to-top (deepest value first), matching the runtime’s env layout and the order the closure body pushes them:

Stack at creation: ( ...rest bottom top )
push_closure pops top-down, then reverses:
  env[0] = bottom (caller's deepest capture)
  env[N-1] = top   (caller's shallowest capture)
Closure function pushes env[0], env[1], ..., env[N-1] in order, so
the body stack looks like ( ...rest bottom top ) — identical to the
caller's visual order.

Single captures are unaffected (a one-element vector reversed is itself).

§Errors

Returns an error if the call site provides more values than the body needs.