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

Allow for pattern matching on anonymous records in a limited context #713

Open
cartermp opened this Issue Jan 16, 2019 · 3 comments

Comments

Projects
None yet
3 participants
@cartermp
Copy link
Member

cartermp commented Jan 16, 2019

I propose we allow for pattern matching on anonymous record types for cases where we already know the definition up front. Here is an example:

type FullName = { FirstName: string; LastName: string }

type Employee =
    | Engineer of FullName
    | Manager of {| Name: FullName; Reports: Employee list |}
    | Executive of {| Name: FullName; Reports: Employee list; Assistant: Employee |}

The compiler knows enough information about the anonymous record types that Manager and Executive are made up of to support pattern matching:

match e with
| Engineer { FirstName= "Phillip" } -> ...
| Manager {| FirstName= "Phillip" |} -> ...
| Executive {| FirstName= "Phillip" |} -> ...
| _ -> ...

Note that the first case is possible today since you can do this with F# records. But the last two cases are not possible today.

However, full-fidelity pattern matching for anonymous records cannot be possible. This is because:

  • No structural subtyping is allowed, since anonymous records are nominal types, limiting their use
  • Compared with "dot"-notation, it's almost always going to be more verbose to use patterns

Pros and Cons

Pros:

  • Enables some more scenarios for anonymous records
  • Brings them to be a bit more at parity with records

Cons:

  • Anonymous records aren't "full" records and so you can't do everything you'd like with them
  • No structural subtyping limits the utility of pattern matching, and anonymous records are not structurally-typed

Extra information

Estimated cost (XS, S, M, L, XL, XXL): M

Related suggestions: (put links to related suggestions here)

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this
@cartermp

This comment has been minimized.

Copy link
Member Author

cartermp commented Jan 16, 2019

Filing this based on discussion here: https://twitter.com/isaac_abraham/status/1085560360990638080

I am personally fine with no pattern matching, but it's worth discussing the issue IMO.

@charlesroddie

This comment has been minimized.

Copy link

charlesroddie commented Jan 17, 2019

So much sugar.

@dsyme

This comment has been minimized.

Copy link
Collaborator

dsyme commented Jan 26, 2019

So much sugar.

I'm trying to decode that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.