Skip to content

Lambdas and Closures

This document is the canonical source for lambda and closure semantics in v0.1.

Lambdas use => and are first-class callable values.

Examples:

let isEven = x => x % 2 == 0;
let add = (i32 x, i32 y) => x + y;
let f = x => {
IO.Println(x.ToString());
return x;
};

Function types use arrow syntax:

(T1, T2) -> TOut

Lambdas are assignable to compatible function types.

  • Lambda parameter types MAY be inferred from an expected function type.
  • If no expected function type exists, parameter types MUST be explicit.
  • Lambda return type is inferred from lambda body when the expected function type is known.
  • Non-capturing lambdas lower as plain callable values.
  • Capturing lambdas are supported under non-escape constraints.
  • Captured environments MUST NOT escape safe scope in v0.1.

17.5 Interaction with query/event features

Section titled “17.5 Interaction with query/event features”
  • Query pipelines MAY accept lambda callables (Where, Select style operators).
  • Event handlers MAY be lambda values if signature-compatible (for event Name(...) / event{N} Name(...) fields subscribed via +=).
  • Query/event sections should reference this document for closure semantics.

Compilation MUST diagnose:

  • missing parameter types when no expected function type exists,
  • incompatible lambda signature at assignment/call site,
  • unsupported escaping captured environments.
  • Syntax examples: docs/spec/lexical-and-syntax.md
  • Type grammar: docs/spec/types.md
  • Inference core: docs/spec/type-inference.md
  • Query contracts: docs/standard-library/Query/Contracts.md
  • Event semantics: docs/spec/events.md