Skip to content

dang: dot-block application, .{{ }} selection, and implicit _ - #101

Merged
vito merged 9 commits into
mainfrom
piping-operator-design
Jun 9, 2026
Merged

vito merged 9 commits into
mainfrom
piping-operator-design

Conversation

@vito

@vito vito commented Jun 6, 2026

Copy link
Copy Markdown
Owner

Collapses the "pipe operator" question into primitives Dang already has, by realigning three things around the .-brace surface. There's no |>, no word operator, and no new bracket — piping is just "a block, applied with .".

What changed

1. _ implicit block parameter (Kotlin it-style). A block with no explicit parameters that references _ gets a single implicit parameter _. Every _ in the block refers to that same one argument, so { _ * 2 } and { _ + _ } both bind one value (not Scala positional params). It works everywhere blocks are used (xs.map { _ * 2 }, xs.filter { _ > 0 }). _ binds to the nearest enclosing param-less block; a nested param-less block shadows it, and a block with explicit params doesn't capture it. A bare _ outside any block stays an undefined-reference error.

2. Object selection moved from .{ } to .{{ }}, mirroring record literals. This surfaces an identity already true in the implementation: selection and record literals both infer to the same anonymous structural type (NewType("", ObjectKind)). Single-field .field is unchanged; only multi-field selects pay the extra brace. Inline-fragment field groups keep their single braces (pets.{{ ... on Cat { name } }}).

3. .{ } now means dot-block application. receiver.{ block } calls the block with the receiver as its single argument, so foo.{ bar(_) } ≡ bar(foo) and foo.{ x => bar(x) } ≡ bar(foo). Because it sits at .'s precedence as a sibling of selection and method calls in SelectOrCall, dot-block interleaves with real method calls in a single chain:

container = Container("app")
  .{ mountCache(_, "/registry", cargoRegistryCache) }
  .{ prepareToolchain(_, "x86_64") }
  .withEnvVariable("CARGO_TARGET_DIR", "/work/target")   # real method
  .{ mountCache(_, "/target", cargoTargetCache) }

The block must take 0 or 1 parameters (2+ is a clear error). None of the three introduces a new type-system concept — they reuse the existing block machinery and the selection/record-literal type identity.

Null semantics (the one gotcha)

.{{ }} and .{ } share the .-brace surface but diverge on null:

  • .{{ }} selection short-circuits: a null receiver propagates null and the result type is nullable.
  • .{ } dot-block does NOT short-circuit: it binds the receiver (even null) to the parameter and runs the block, letting the block decide.

This is documented prominently in blocks.md and graphql.md.

Migration

All ~95 existing selection sites across tests/ and demos/ were migrated to .{{ }} via a brace-matching codemod. mod/doug/main.dang was intentionally left on single-brace syntax — it's Dang code executed by the dagger engine's pinned dang SDK (engineVersion v0.20.1), which predates {{; it can migrate once the engine bumps to a release including this change. .dagger has no selections and is unaffected.

Verification

dagger check is fully green: docs, lint, go tests (992 passed / 0 failed), tree-sitter corpus, and the generate freshness check. New assert-based tests cover the implicit _ (map/filter/each, { _ + _ } single-binding, nested shadowing, explicit-param blocks, bare-_ error), dot-block application (implicit/explicit params, null receiver, multi-param rejection, chaining, the .{ {{...}} } adjacency case), and a Cargo-style interleaving smoke test.

Notes / follow-ups

  • Editor _-highlighting in the nvim/zed tree-sitter queries (separate submodule repos) was not added; highlight-compatibility tests pass as-is and _ is an ordinary identifier in the grammar.
  • The formatter collapses multiline mixed dot-block/method chains onto fewer lines (round-trips correctly, tests pass, but the layout isn't as polished as a pure method chain). Faithful multiline layout for interleaved chains is a separate formatter enhancement.

🤖 Generated with Claude Code

@cloudflare-workers-and-pages

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

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: 12e902c
Status: ✅  Deploy successful!
Preview URL: https://6edc54d0.dang-3kk.pages.dev
Branch Preview URL: https://piping-operator-design.dang-3kk.pages.dev

View logs

vito added 9 commits June 9, 2026 14:09
A block with no explicit parameters that references `_` now takes a
single implicit parameter named `_`, Kotlin `it`-style: every `_` in
the block refers to the same single argument, so `{ _ * 2 }` and
`{ _ + _ }` both bind one value. This is detected in BlockArg.Infer,
which gives the block one implicit param (using the expected param type
when available) and binds it in the inference scope; eval then binds the
incoming value to `_` via the inferred parameter record.

Detection respects nesting: a reference inside a nested param-less block
belongs to that inner block, which shadows the outer `_`. Blocks with
explicit params do not capture `_`, so a `_` inside them resolves
outward. A bare `_` outside any block remains an undefined reference.

Signed-off-by: Alex Suraci <[email protected]>
Object selection now opens and closes on double braces, mirroring record
literals (`{{ }}`) — both already infer to the same anonymous structural
type, so the notation now reflects that identity. This frees the single
`.{ }` form for dot-block application in a following change.

The grammar change is confined to the ObjectSelection rule; inline
fragment field groups (`... on T { ... }`) keep their single braces.
Nested selections recurse through ObjectSelection, so they pick up the
double braces too. The formatter emits `{{ }}` to match, and all
existing selection sites across tests, mod, and demos are migrated.

Signed-off-by: Alex Suraci <[email protected]>
`receiver.{ block }` now calls the block with the receiver as its single
argument, so `foo.{ bar(_) }` and `foo.{ x => bar(x) }` both mean
`bar(foo)`. Combined with the implicit `_` parameter, this is the
language's piping primitive: a block, applied with a dot. Because it
sits at `.`'s precedence as a sibling of selection and method calls in
SelectOrCall, dot-block interleaves with real method calls in a single
chain.

A new DotApply node infers the receiver type, passes it as the block's
single parameter (the block must take 0 or 1 params; 2+ is a clear
error), and yields the block's body type. Unlike selection, dot-block
does not short-circuit on null: the receiver is bound even when null and
the block decides what to do. Selection still wins the `.{{` form via
ordered choice, and a space (`.{ {{...}} }`) lets a dot-block return a
record literal unambiguously. The formatter emits the receiver followed
by the block.

Signed-off-by: Alex Suraci <[email protected]>
The tree-sitter grammar is derived from the PEG, so regenerating picks up
the selection move to double braces and the dot-block alternative in
SelectOrCall. Regenerated grammar.json, node-types.json, and parser.c;
the corpus tests still pass.

Signed-off-by: Alex Suraci <[email protected]>
Updates the language docs for the selection/dot-block realignment.
Selection examples move to double braces (inline-fragment field groups
keep their single braces). blocks.md gains sections for the implicit `_`
parameter and for dot-block application as Dang's piping primitive,
including the chain-interleaving example. operators.md notes the two
postfix `.`-brace forms at `.` precedence, and grammar.md describes the
ObjectSelection and DotBlock productions.

The null-semantics divergence is called out where both appear: `.{{ }}`
selection short-circuits on null while `.{ }` dot-block binds the
receiver (even null) and lets the block decide.

Signed-off-by: Alex Suraci <[email protected]>
Exercises dot-blocks interleaved with a real method call in a single
chain, mirroring the design discussion's Cargo module example, to guard
the headline interleaving behavior end to end.

Signed-off-by: Alex Suraci <[email protected]>
Reframes the null section so it follows from what dot-block is — block
application, not navigation — rather than reading as a gotcha versus
selection. A null receiver is simply passed into the block (`bar(null)`),
which is what lets a block handle null; selection navigates fields and so
short-circuits. Same `.`-brace surface, different jobs.

Signed-off-by: Alex Suraci <[email protected]>
Dang has no `?.` optional-chaining operator, so the example was not valid
syntax. The null-coalesce and explicit-null-check examples already show
how a block handles null; remove the bogus one.

Signed-off-by: Alex Suraci <[email protected]>
Regenerated docs/js/tree-sitter-dang.wasm from the rebased grammar via
the pinned dagger toolchain so the playground highlighter matches the
`.{{ }}` / `.{ }` grammar.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito force-pushed the piping-operator-design branch from 3dd52fa to 12e902c Compare June 9, 2026 14:13
@vito
vito merged commit ac1cbc3 into main Jun 9, 2026
8 checks passed
vito added a commit that referenced this pull request Jun 10, 2026
PR #101 changed `.{ }` from object selection to dot-block application,
which breaks Dang code written before the merge. v1.0.0 was tagged from
release/v1 with that change reverted so existing modules have a stable
line to pin; main keeps the new syntax and becomes v2.

Bump the module path to github.com/vito/dang/v2 so the breaking change
requires an explicit opt-in from dependents, per Go's semantic import
versioning. The docs module keeps its own github.com/vito/dang/docs
path, with its require/replace of the root module updated to /v2.
Install instructions in the docs now reference the /v2 path.

Signed-off-by: Alex Suraci <[email protected]>
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