Lambdas and Closures
This document is the canonical source for lambda and closure semantics in v0.1.
17.1 Lambda forms
Section titled “17.1 Lambda forms”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;};17.2 Type model
Section titled “17.2 Type model”Function types use arrow syntax:
(T1, T2) -> TOutLambdas are assignable to compatible function types.
17.3 Inference rules
Section titled “17.3 Inference rules”- 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.
17.4 Capture and closure rules
Section titled “17.4 Capture and closure rules”- 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,Selectstyle 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.
17.6 Diagnostics (required)
Section titled “17.6 Diagnostics (required)”Compilation MUST diagnose:
- missing parameter types when no expected function type exists,
- incompatible lambda signature at assignment/call site,
- unsupported escaping captured environments.
17.7 Cross references
Section titled “17.7 Cross references”- 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