rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

[PoC] Allowing ad hoc aliasing of an or pattern of literals into a polyvariant

Open zth opened this issue 3 months ago • 1 comments

Now that the runtime represenation of polyvariants is stable and well defined, we can allow aliasing or patterns of literals into a polyvariant if we want, since we can guarantee that if it matches it has been refined to the provided runtime representation.

Example:

type abc = [#a | #b | #c]

let useABC = (x: abc) =>
  switch x {
  | #a => 1
  | #b => 2
  | #c => 3
  }

let fromString = s =>
  switch s {
  | ("a" | "b" | "c") as #...f => useABC(f) // `f` will be a polyvariant of the literals matched on in the pattern. So `[#a | #b | #c]`.
  | _ => 0
  }

Notes

For the implementation, we might need to check extra that the literals are valid as polyvariant constructors.

zth avatar Aug 24 '25 20:08 zth

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7801
@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@7801
@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@7801
@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@7801
@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@7801
@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@7801

commit: 487cea6

pkg-pr-new[bot] avatar Aug 24 '25 20:08 pkg-pr-new[bot]