Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type spreads of regular variants in patterns #6721

Open
wants to merge 7 commits into
base: 11.0_release
Choose a base branch
from

Conversation

zth
Copy link
Collaborator

@zth zth commented Apr 10, 2024

Solves part of #6273
Closes #6562
Closes #6277

This PoC adds support for type spreads of regular variants in patterns. This is already a thing with polyvariants, and now it's also a thing for regular variants.

Rationale is we already have variant spreads and coercion, which lets you go from small type to big type. Having this will let you go from big type to small type. This is a missing piece.

Specification:

  • Any variant X that is a subtype of variant Y can be spread in a pattern matching of a value of type Y
  • Using an alias ...subtypeVariant as value will give the type subTypeVariant to value
  • Syntax: Spreads of regular variants are done like spreads of polyvariants, but without the leading #: | ...subtypeVariant as value => doSomethingWithSubTypeVariant(value)
type a = One | Two | Three
type b = | ...a | Four | Five

let doWithA = (a: a) => {
  switch a {
  | One => Js.log("aaa")
  | Two => Js.log("twwwoooo")
  | Three => Js.log("threeeee")
  }
}

let lookup = (b: b) =>
  switch b {
  | ...a as a => doWithA(a)
  | Four => Js.log("four")
  | Five => Js.log("five")
  }

Should be a well isolated feature, since it's dependent on the parser adding an attribute when the spread is a regular variant (no leading #) and no regular pre-existing code path is touched.

let path, decl = Typetexp.find_type env lid.loc lid.txt in
match decl with
| {type_kind = Type_variant constructors} -> (
(* TODO: Probably problematic that we don't account for type params here? *)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cristianoc did you see this here? That we're not caring about type params. Type params aren't allowed in spreads anyway right now so maybe it doesn't matter.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth checking that there are no type params, so it's not overlooked in future.

@zth zth force-pushed the variant-pattern-matching-type-spread branch from 231f131 to a77e7b1 Compare April 11, 2024 06:03
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.

None yet

2 participants