Skip to content

Feat/ternary syntax#3

Open
jwulf wants to merge 2 commits into
Doorman11991:mainfrom
jwulf:feat/ternary-syntax
Open

Feat/ternary syntax#3
jwulf wants to merge 2 commits into
Doorman11991:mainfrom
jwulf:feat/ternary-syntax

Conversation

@jwulf

@jwulf jwulf commented Jun 3, 2026

Copy link
Copy Markdown

This PR adds support for ternary syntax using the nullish coalescing operator ??.

New syntax: ?? null-coalescing operator

The ?? operator returns its left operand when that value is present, and
falls back to the right operand otherwise. It's intended for capability
effects and requires, typically to apply optional update parameters:

system Catalog {
  entity Product {
    owns: [
      title:       string,
      price_cents: uint,
    ]
  }

  capability update_product(
    product:     Product,
    title:       optional<string>,
    price_cents: optional<uint>
  ) {
    requires: [caller.role == "admin"]
    effects: [
      product.title       = title ?? product.title,
      product.price_cents = price_cents ?? product.price_cents
    ]
    sync: transactional
  }
}

title ?? product.title keeps the existing title when no new title is
supplied. It type-checks as the unwrapped (non-optional) type — e.g.
optional<string> ?? string yields string — and lowers to the JavaScript
null-coalescing operator in generated code:

const __effect_0 = await query(
  `UPDATE products SET title = $1, updated_at = NOW() WHERE id = $2 RETURNING *`,
  [(title ?? product?.title), req.params.id]
);

Scope / limitations

?? is not allowed in entity or top-level constraints, which compile to
SQL (use COALESCE there instead). Using it in a constraint is a compile-time
error:

entity Product {
  owns: [price_cents: optional<uint>]
  constraints: [(price_cents ?? 0) > 0]   // ❌ T013: '??' not allowed in constraints
}

jwulf added 2 commits June 3, 2026 13:33
Signed-off-by: Josh Wulf <josh@magikcraft.io>
Signed-off-by: Josh Wulf <josh@magikcraft.io>
@jwulf jwulf force-pushed the feat/ternary-syntax branch from a65833c to 5f677c2 Compare June 3, 2026 01:33
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