Skip to content

ADR 0070: Functions as values, without capture

Status: Final Type: Feature Created: 2026-11-05 Amends: 0014 (appends IR20) Amended-By: 0076 (a function value is a counted closure object, its code called with the closure first), 0079 (the WebAssembly-text backend retired) Related-ADRs: 0010 (the nested def lifted with its captures as parameters, which a lambda bound to a name reuses), 0007 (the pending check after a call), 0024 (a returned value is owned by the caller), 0029 (what a union may hold)

Context

Two Shed Skin programs in the census stop at functions used as values, and mypy --strict accepts both, so under A180 both refusals are distance:

  • mastermind2 passes five module-level functions (s_bestinfo, s_allrand, …) to a parameter annotated Callable[[int, list[list[int]]], list[int]] and calls them through it.
  • tonyjpegdecoder binds three lambdas to local names (FIX = lambda x, n: int(x * n + 0.5)) and calls them 26 times. It never passes them anywhere.

postpile has no function type. A name is a callee only when it is called (f(...)). A nested def is lifted to a top-level function whose captures become leading parameters (ADR 0010), so calling it is a direct call. The pygo TaskFn is a function address, but only tg.spawn can see one.

Decision

  1. The type. Callable[[A, B], R], from collections.abc or typing, is a function type: one dtype per signature, where the signature is the parameter and return types with the member types a slot records (a list[int] is not a list[str]). Two spellings of one signature are one type. R may be None. These are refused: Callable[..., R], a parameter or return that is a tuple or an array, and a parameter list that is not a list literal.
  2. Where a function value can be. Only in a parameter, a local or a return. A field, a list, dict or set member, a tuple component, a union case, a module-level global and a pygo task argument are refused, each with a diagnostic. Nothing else can be done with one but bind it, pass it, return it and call it: printing, comparing, hashing or testing it for truth are refused, as they are for any value with no operators.
  3. Where a function value comes from. A function this module defines at the top level, named without calling it (eval_strategy("s_bestinfo", s_bestinfo)), is the address of that function. Its parameters and return must be exactly those of the slot that receives it; def f(x: int, y: int = 0) is not a Callable[[int], R], though mypy accepts it, because the call through the value would pass one argument where f takes two. A function of another module, a method, a generator, a builtin, a function of the standard library or one a CPython import binds cannot be a value yet. Neither can a nested def that captures a local, whose captures have nowhere to go.
  4. A call through a function value is an indirect call with the value's signature: arguments bound positionally (a Callable names no parameters, so a keyword is refused, as mypy refuses it), checked and converted as a direct call's are, the result owned as a direct call's is. It is followed by the pending check of ADR 0007 whatever the callee. The function making it can raise as well. The compiler cannot see which function a value holds; a check that is never taken costs a branch, where a missing one swallows an exception.
  5. A lambda bound to a name. FIX: Callable[[float, int], int] = lambda x, n: int(x * n + 0.5) inside a function is lowered as the nested def it means:

    def FIX(x: float, n: int) -> int:
        return int(x * n + 0.5)
    

    The annotation gives the lambda its types; the lambda has plain parameters, as many as the annotation names, without defaults. It is lifted as ADR 0010 lifts a nested def, captures included and refused under the same rule (a local the function binds again, or binds after the lambda, would differ between Python's cell and the copy). A call FIX(...) is a direct call. Used as a value, it is the lifted function's address, which requires no captures. The name must be bound only by that statement. A lambda bound without an annotation stays refused: typing it from the calls is inference postpile does nowhere else (A184); the census writes the annotation from the types its runs observed. 6. Representation. A function value is the function's address: a C function pointer of a generic type, cast to the call's signature at the call (an ISO C conversion between function pointer types), a QBE l and an LLVM ptr. It is not counted and owns nothing. Two IR instructions handle it: FuncAddr takes a function's address, and CallIndirect calls through a value with arguments and result typed like a Call's. The verifier checks that a FuncAddr names a function of the module and that a CallIndirect's arguments and result match the signature the callee's dtype records.

Consequences

  • mastermind2 and tonyjpegdecoder, the two census programs that stopped at functions as values (A184), get past them. tonyjpegdecoder compiles; mastermind2 is refused since A190 for an unrelated reason, an int assigned to a float local.
  • A program that uses no function value emits exactly the IR it did before. A function that calls through a value checks the pending slot after that call and is a raiser, so the callers check too.
  • Capture, a function stored in a container or a field, a bound method as a value and Callable[..., R] wait for a program that needs them. A capture needs an environment, which the address has no room for; it is A184's line.