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

Ast_builder.Default.pexp_tuple on empty list #435

Open
sim642 opened this issue Jun 25, 2023 · 1 comment
Open

Ast_builder.Default.pexp_tuple on empty list #435

sim642 opened this issue Jun 25, 2023 · 1 comment

Comments

@sim642
Copy link
Contributor

sim642 commented Jun 25, 2023

Ast_builder.Default.pexp_tuple, etc. currently have a special case for the one-element list:

let pexp_tuple ~loc l = match l with [ x ] -> x | _ -> pexp_tuple ~loc l
let ppat_tuple ~loc l = match l with [ x ] -> x | _ -> ppat_tuple ~loc l
let ptyp_tuple ~loc l = match l with [ x ] -> x | _ -> ptyp_tuple ~loc l
let pexp_tuple_opt ~loc l =
match l with [] -> None | _ :: _ -> Some (pexp_tuple ~loc l)
let ppat_tuple_opt ~loc l =
match l with [] -> None | _ :: _ -> Some (ppat_tuple ~loc l)

I wonder if it makes sense to also have a special case for the empty list, degrading to () instead. Right now calling them with the empty list yields an error from deeper down that a tuple needs at least 2 elements.

Or maybe there's a reason this generalization hasn't been made? The existence _opt versions of these functions hints at the need to distinguish the empty list case.

@pitag-ha
Copy link
Member

Thanks for the issue! I wasn't sure myself, so I've just had a look at the compiler Parsetree module. There are few Parsetree invariants that aren't enforced by the type checker. They're written down as comments in the compiler Parsetree module. There's one for Pexp_tuple: n>=2. That explains the behavior of ppat_tuple and why ppat_tuple_opt exists.

It would be quite valuable to add documentation about that in Ast_builder though. Do you want to do that?

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

No branches or pull requests

2 participants