Skip to content

Replace the for keyword with a loop builtin - #108

Merged
vito merged 4 commits into
mainfrom
loop-not-for
Jun 11, 2026
Merged

vito merged 4 commits into
mainfrom
loop-not-for

Conversation

@vito

@vito vito commented Jun 11, 2026

Copy link
Copy Markdown
Owner

What

Replaces the for keyword with a loop stdlib builtin, then removes for entirely.

for only ever looped on a condition — the name promised iteration that the language deliberately routes through .each instead. Rather than rename it to while, this moves repetition into the library alongside .each/.map, where it always belonged:

let answer = loop { break 42 }            # loop is a builtin, not a keyword
loop { if (done) { break } ... }          # conditional loops use a mid-body guard

Why a builtin works for free

The existing control-flow machinery is fully generic: break is caught at the call site of any block-taking call, continue resolves at block-invocation level, and mergeCallBreakTypes types the call result from the break values. The builtin's implementation is just an infinite Go loop calling the block — no new inference concepts, no parser special cases.

The result typing actually improves over for: loop declares a bare type variable that only materializes through break merging, so loop { break 42 } types directly as Int!, where for { break 42 } was forced nullable.

Removal fallout

  • LoopFrame is gone — block-call frames are the only break/continue targets left.
  • Condition-loop nullability rules (and their error tests) are obsolete.
  • Loop-condition narrowing (for (x != null) { ... }) is gone; the guard form (if (x == null) { break }) narrows identically via divergence.
  • Reaching for the old keyword now reports "for" not found at the call site (pinned by an error test).
  • Tests migrated to loop/guards; tree-sitter grammar regenerated; zed/nvim highlight queries (submodules pushed to their mains), VSCode grammar, REPL completions, docs, and the language-reference skill all updated. Also dropped in from REPL completions — it was never a grammar token.

Technically a breaking change shipped without a major bump: nothing outside Dang's own tests used for, and Dang's semver is currently driven entirely by Dagger's usage.

Note: the zed extension's grammar submodule still pins an older dang commit that parses for; the updated highlight query is compatible with both, so that bump can follow whenever.

🤖 Generated with Claude Code

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 11, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: bcc7faf
Status: ✅  Deploy successful!
Preview URL: https://71c2b155.dang-3kk.pages.dev
Branch Preview URL: https://loop-not-for.dang-3kk.pages.dev

View logs

vito added 2 commits June 11, 2026 00:34
Add loop, a global block-taking builtin that calls its block repeatedly
forever, exiting only via break (or return/raise) unwinding out of it. It
is the builtin equivalent of the infinite `for` form, continuing the
language's direction of moving repetition into the library alongside
.each/.map rather than dedicating a keyword to it.

The existing control-flow machinery handles everything generically: break
is caught at the call site, continue resolves at block-invocation level,
and mergeCallBreakTypes types the call result from the break values. The
declared return type is a bare type variable that only materializes
through break merging, so `loop { break 42 }` types as Int! — unlike the
`for` keyword, whose result is always nullable.

This paves the way for removing the `for` keyword, whose name promises
iteration the language intentionally routes through .each instead.

Signed-off-by: Alex Suraci <[email protected]>
Dang's `for` only ever looped on a condition — the name promised
iteration the language deliberately routes through .each — and the new
`loop` builtin covers everything it did, with better result typing.
Remove the keyword outright rather than deprecating: no production code
used it (only Dang's own tests), and Dang's semver is currently driven
entirely by Dagger's usage.

Conditional loops are now written with a mid-body guard, which the test
migrations show reads fine: `loop { if (!cond) { break } ... }`. The
LoopFrame control-frame kind goes away since block-call frames are the
only break/continue targets left, and condition-loop nullability rules
(and their error tests) are obsolete. A new error test pins what users
hit if they reach for the old keyword: `"for" not found`.

Also drop `for`/`in` from REPL keyword completion (`in` was never a
token), regenerate the tree-sitter grammar, and update the highlight
queries, VSCode grammar, docs, and language-reference skill to match.

Signed-off-by: Alex Suraci <[email protected]>
vito added 2 commits June 11, 2026 00:44
Signed-off-by: Alex Suraci <[email protected]>
A bare reference auto-calls, so referencing a function that requires a
block argument is the same mistake as calling it without one — but it
silently produced the function value instead, while the parenthesized
call errored. The inconsistency surfaced when `dang fmt` rewrote the
`loop()` error test to bare `loop` and the expected error vanished.
Function references without calling are already spelled `&name`, so the
bare form has no legitimate meaning.

autoCallFnType now returns an error for block-taking function types,
and every auto-call inference site propagates it. This also rescues the
previously confusing goldens for the existing cases: `numbers.map`
without a block now reports `function requires a block argument`
instead of `cannot use (&block: (item: Int!, index: Int!): b): [b]! as
[Int!]!`, and constructors/user functions match.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito merged commit a559531 into main Jun 11, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant