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

Closed
5 tasks done
cartermp opened this issue Jan 16, 2019 · 11 comments
Closed
5 tasks done

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

cartermp opened this issue Jan 16, 2019 · 11 comments

Comments

@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
Copy link
Member Author

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
Copy link

So much sugar.

@dsyme
Copy link
Collaborator

dsyme commented Jan 26, 2019

So much sugar.

I'm trying to decode that :)

@eiriktsarpalis
Copy link

I'd love to see this feature added.

@vbfox
Copy link

vbfox commented Sep 11, 2019

Would this also allow for deconstruction of them automatically or does it need to be suggested separately ?

let x = {| bar = 42 |}
let { bar = bar } = x // Or let {| bar = bar |} = x
printfn "%A" bar

@Swoorup
Copy link

Swoorup commented Feb 19, 2020

Is there a reason why anonymous record does not support structural subtyping?

@smoothdeveloper
Copy link
Contributor

@Swoorup can you provide the non-initiated what would be the semantics of substructural typing in terms of a short pseudocode sample?

I'm browsing https://en.wikipedia.org/wiki/Substructural_type_system but it seems there is lot under that term.

In any case, anonymous record design closely follows the essence of C# anonymous types, it may explain why this is basically not more than what it is now.

I also think they have the same constraint of not being unified across assembly boundary, due to the same origin / implementation choice of the feature.

@Swoorup
Copy link

Swoorup commented Feb 19, 2020

@smoothdeveloper 🤦‍♂️ Sorry I meant to say structural subtyping not substructural type system.

type Person = {|
  name: string;
|}

let developer = {|
  name: 'Joe',
  language: 'Typescript',
|}

// this is ok because Person is a subtype of typeof developer
let otherDeveloper: Person = developer

The above would have been valid, if subtyping was supported

@smoothdeveloper
Copy link
Contributor

@Swoorup just my guess, but above C# siblings reason and also, the concepts of subtyping and normal records in F#, I feel, are intendedly very separated.

Not exposed to it much, but typescript has a type system that leans very well for laying type safety on top of javascript.

@cartermp
Copy link
Member Author

@Swoorup See the section in the RFC about structural subtyping here: https://github.com/fsharp/fslang-design/blob/master/FSharp-4.6/FS-1030-anonymous-records.md#design-principle-no-structural-subtyping

@dsyme
Copy link
Collaborator

dsyme commented Apr 12, 2023

I'm never been keen to expand pattern patching to anonymous records, and indeed am somewhat ambivalent about pattern matching on records at all.

I'll close this on that basis.

@dsyme dsyme closed this as completed Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants