Skip to content

CEP-0017: Function Literals and Callable Values

Draft

Draft proposal for anonymous function expressions, bound method values, closure values, and defaults through callable values. V1 has function items and function pointers only.

Summary

Catalyst should eventually decide how anonymous functions, bound methods, callable values, and possibly closures fit with the V1 distinction between comptime-known function items and runtime-storable function pointers.

V1 function items are compile-time-known definitions. Runtime storage uses explicit function pointer types. Capturing closures and bound method values are deferred.

Example

Possible future shape:

const doubled = items.map(fn (x: i32) i32 {
  return x * 2
})

const threshold = 10
const filtered = items.filter(fn (x: i32) bool {
  return x > threshold
})

The first literal can be non-capturing. The second captures threshold, so it needs the future closure/capture model rather than a plain V1 function pointer.

Motivation

Callable values affect ABI shape, capture ownership, resource cleanup, default parameters, and overload/function-pointer selection. They should not be bolted onto function pointer syntax without a clear storage and lifetime model.

Proposed Direction

The proposal should cover:

  • anonymous function expression syntax;
  • return inference for function literals with bodies;
  • bound method values and receiver capture;
  • closure capture semantics;
  • move, copy, and resource behavior for captures;
  • runtime-storable callable value representation;
  • function defaults through callable values;
  • coercion to function pointers when no capture exists;
  • interaction with CEP-0004: Function Overloading.

V1 Compatibility

V1 rejects anonymous function expressions, bound method values, and capturing closures. A function item can coerce to a runtime function pointer only in accepted expected-type contexts.