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

record punning & record patterns #389

Open
wants to merge 5 commits into
base: dev-0-1-0
Choose a base branch
from
Open

Conversation

leque
Copy link
Contributor

@leque leque commented Jan 3, 2023

closes #358 and #359.

This PR implements record punning and record patterns in 0.1.0 branch.

Record punning is a short-hand notation for record construction. You can omit a field value if it is a variable reference and the variable has the same name as a record field. E.g. (| x = x |) can be written as (| x |).

Record pattern enables us to pattern match against record values. Record patterns are typed as described in #358:

  % fx : (| x : 'a |) -> 'a
  val fx (| x |) = x

  % fx- : (| x : 'a | ?'b |) -> 'a
  val fx- (| x, _ |) = x

Some remarks:

  • --bytecomp routine is implemented, but is not tested since 0.1.0 does not support eval_libary_file in bytecomp mode yet.
  • Exhaustiveness check is not implemented because I do not understand what algorithm is used in Exhckecker. It would be nice to implement it as a separated PR.
  • This PR turns tests/parsing tests into inline tests to prevent the possibility that inserting tests changes locations of other tests.

}
;
pat_record_body:
| WILDCARD; COMMA?
Copy link
Owner

Choose a reason for hiding this comment

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

The description COMMA? allows (| foo = x, _, |) for a record pattern as well as (| foo = x, _ |). Is this intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is intentional. I think there is no reason to forbid a trailing comma after a wildcard.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI: OCaml also allows ; after _ in record patterns, but forbids ; after .. in object types; i.e. {x; _;} is acceptable, <x: int; ..;> is not. This PR's syntax is derived from OCaml's record pattern syntax.

Copy link

Choose a reason for hiding this comment

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

Same for list [ value1; ] python also accept trailing comma: one advantage is we can swap item and it will just works. Other reason seems to be human better deal with terminator than separator (last argument was given by an ocp-indent author).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants