Skip to content
LogoLogo

Control Flow

Normative direction for expression forms and scope exits

Status: core expression orientation is locked. Labeled values, loop else, detailed typing, and some grammar are direction.

Existing TypeScript Forms

Valid TypeScript statement forms MUST retain their TypeScript behavior on the TypeScript target.

Adding expression forms MUST NOT reinterpret an existing TypeScript statement in a way that changes observable behavior.

Expression Forms

The following constructs MUST be usable as expressions:

  • blocks
  • if
  • switch
  • while
  • for

An expression-form construct MUST have a statically determined success type from its reachable value-producing exits.

If

An if expression MUST select exactly one reachable branch. When used where a value is required, every normal completion path MUST produce a compatible value.

const value = if (condition) consequent else alternate

The alternate branch MAY be omitted only where the resulting unit or optional semantics are explicitly defined by the final grammar.

Switch

Switch syntax MUST use the same switch, case, default, and colon-delimited clauses as TypeScript. VibeLang MUST NOT introduce a separate arrow-arm switch grammar.

In expression position, a switch MUST return the selected case's final expression. Expression switches MUST NOT fall through between cases. Statement-position switches MUST retain TypeScript's existing fallthrough and break behavior.

When the scrutinee is a known closed union, the compiler SHOULD require an expression switch to be exhaustive.

Blocks

An expression block MAY produce a value from its final expression or from a labeled value break.

A declaration inside an expression block follows ordinary lexical scope.

Labeled Break Values

Direction: a break targeting an expression label MAY carry a value:

break :label value

The compiler MUST combine reachable break values when determining the labeled expression's type.

Loop Else

Direction: a loop expression MAY provide an else value for the path where the loop completes without breaking to its value label.

The exact treatment of infinite loops, unreachable completion, and nested label selection is not finalized.

Throw Statements

The initial language MUST retain ordinary statement-form throw and MUST NOT add a VibeLang-specific throw-expression grammar. Inside a fallible function, throwing an Error produces the function's Result error variant according to Failure Semantics.

Declarations in Conditions

VibeLang MUST track relevant TC39 work for declarations in conditionals and SHOULD adopt it early when compatible with lexical scope and TypeScript parsing.

Deferred Cleanup

defer MUST register cleanup for scope exit. errdefer MUST register cleanup for a Result error exit.

The language MUST define deterministic ordering for multiple deferred operations. The candidate is last-in, first-out, but this is not yet recorded as locked.

Async suspension, defects, cancellation, and cleanup failures require further specification.