Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[PoC][Post V12] Tagged union primitive catch all#7996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
zth wants to merge1 commit intomaster
base:master
Choose a base branch
Loading
fromtagged-variant-primitive-catch-all

Conversation

@zth
Copy link
Member

@zthzth commentedOct 27, 2025

Something to discuss post v12

Add support for providing "catch all" variant constructors to tagged variants, to allow capturing all non-literal discriminator cases of a variant. This lets you write a variant that can enumerate a few known discriminators, and "catch" the rest at runtime in a single catch-all constructor, for those runtime values you haven't statically enumerated.

Rationale

Consider a common interop type with a numeric discriminant where only a few literal values are special, and everything else should go to “Other”:

@tag("status")typeresponse=  | @as(202)Ok202({})  | @as(200)Ok200({data:JSON.t})  | @as(int)Other({status:int,body:string})letdecode= (x:response)=>switchx {  |Ok202(_)=>Console.log("202 received")  |Ok200({data})=>Console.log2("200 received with data:",data)  |Other({status,body})=>Console.log3("Other received with status:",status,"and body:",body)// All other status codes end up here  }

Before this feature, there was no elegant way to express “everything else that’s a number” for a tagged variant. You either had to hand‑write a decoder that inspectsx.status and constructs a variant by enumerating all literal cases yourself, or attempt exhaustive matches that still couldn’t represent unknown future values. Hand‑written decoding is tedious, error‑prone, and typically forces you to duplicate the discriminant logic outside the type just to avoid runtime match failures.

With a primitive catch‑all annotation like@as(int|float|string), the type itself states the intent. The compiler generates literal‑first checks over the discriminant field and only then falls back to a single primitive “catch all” branch. This captures the real‑world use case (you only care about a few literals; everything else of that primitive kind is one case), avoids bespoke decoders, and stays sound even when a particular match expression doesn’t enumerate all literal members.

fhammerschmidt, illusionalsagacity, and tsnobip reacted with heart emoji
@cristianoc
Copy link
Collaborator

Tried to paste the example in a prompt and ask for what JS this produces.
The result was confusion.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@zth@cristianoc

[8]ページ先頭

©2009-2025 Movatter.jp