Skip to content

Add Maybe.some / Maybe.none constructors with T->Maybe<T> coercion#186

Open
WalterGropius wants to merge 4 commits into
vercel-labs:mainfrom
WalterGropius:feat/maybe-ctor
Open

Add Maybe.some / Maybe.none constructors with T->Maybe<T> coercion#186
WalterGropius wants to merge 4 commits into
vercel-labs:mainfrom
WalterGropius:feat/maybe-ctor

Conversation

@WalterGropius
Copy link
Copy Markdown

Summary

Adds explicit Maybe.some(value) and Maybe.none constructors, mirroring
the choice-style pattern, and lets a plain T value coerce to
Maybe.some(value) in any position that already expects Maybe<T>
(return, let-with-annotation, or argument). The existing .has/.value
member access and check maybe semantics are unchanged.

Source

Addresses B3. Reproduced against main @ 999a46b:

  • fun a(x:u64)->Maybe<u64>{return x} failed with TYP003.
  • return Maybe.some(x) failed with NAM003 because Maybe was not a
    visible symbol.

Changes

  • native/zero-c/src/checker.c: recognize Maybe.some(value) and
    Maybe.none, type them as Maybe<T>, and add a T -> Maybe<T>
    coercion in check_expr_expected (covers EXPR_NUMBER and any other
    expression whose actual type is compatible with T). A new TYP011
    message variant fires when Maybe.none lacks an explicit
    Maybe<T> context.
  • native/zero-c/src/ir.c: lower Maybe.some(literal), Maybe.none,
    and the T -> Maybe<T> integer literal coercion to the existing
    IR_VALUE_MAYBE_SCALAR_LITERAL. No new IR opcodes were added, so
    every backend (ELF64, Mach-O, COFF, AArch64 MVP) continues to work
    without changes. Non-literal Maybe.some payloads and non-scalar
    element types fall back to CGEN004 like other MVP gaps.
  • native/zero-c/src/main.c: register a zero explain TYP011 entry
    covering the expanded message.
  • docs/articles/language-reference.md,
    docs/articles/diagnostics.md: document the new constructors and the
    refreshed TYP011 scope.

Conformance

  • conformance/native/pass/maybe-some.0: build present and absent
    Maybe via the new constructors and check .has.
  • conformance/native/pass/maybe-coerce-return.0: function returning
    Maybe<u8> via plain T and null coercion.
  • conformance/native/fail/maybe-bare-without-context.0: bare
    Maybe.none is rejected with TYP011.
  • node conformance/run.mjs and
    node --experimental-strip-types scripts/snapshot-command-contracts.mts
    both pass locally.

Proposed CHANGELOG line

  • Add Maybe.some(value) and Maybe.none constructors and accept plain T in Maybe<T> positions.

WalterGropius and others added 4 commits May 21, 2026 23:29
Mirrors the choice-style constructor pattern so users can construct
present Maybe values explicitly with Maybe.some(value) or Maybe.none.
In a context that expects Maybe<T>, a plain T value coerces to
Maybe.some(value); null and Maybe.none remain Maybe absences.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…ercion

Reuses the existing IR_VALUE_MAYBE_SCALAR_LITERAL opcode so no new IR
or aarch64 lowering is required. Non-literal Maybe.some payloads and
non-scalar element types fall back to CGEN004 like other MVP gaps.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Adds runtime-or-unsupported fixtures for Maybe.some and the
plain-T-to-Maybe<T> coercion in returns, plus a TYP011 fail fixture for
bare Maybe.none without an explicit context. Refreshes the
language-reference Maybe paragraph to mention the new constructors.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The TYP011 message now covers both bare null and bare Maybe.none, so
zero explain TYP011 surfaces a short rationale and a paired
bad/good example.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@vercel
Copy link
Copy Markdown

vercel Bot commented May 21, 2026

@WalterGropius is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

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