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-like member constraints #717

Open
cannorin opened this Issue Jan 24, 2019 · 0 comments

Comments

Projects
None yet
1 participant
@cannorin
Copy link

cannorin commented Jan 24, 2019

Record-like member constraints

I propose we allow record-like syntax sugar for member constraints:

let inline f (x: ^TX when ^TX :> {| X: ^X; Y: ^Y1 -> ^Y2 |}) = ...

instead of

let inline f (x: ^TX when ^TX: (member X: ^X) and ^TX: (member Y: ^Y1 -> ^Y2)) = ...

Pros and Cons

Pros:

  • It makes member constraints easier to read and write, especially when we have many member constraints to one type.

  • Combined with (anonymous) records, it can emulate structural subtyping with clean syntax:

let inline getX (x: ^TX when ^TX :> {| X: ^X |}) = ...

getX {| X = 0 |}
getX {| X = 1; Y = "foo" |}
...

Cons:

  • It's very confusing with some existing features.
    • It uses the same syntax with anonymous records and type constraints.
      • maybe we should add some prefix like ^TX :> interface {| X: ^X |} or ^TX :> shape {| X: ^X |}
      • or borrow the syntax proposed in #483: ^TX : #{| X: ^X |}
    • Although it resembles (anonymous) records, it can actually be used to arbitrary types i.e. classes.
  • It is nearly useless unless we have #440. We still have to write (^TX: (member X: ...) x) to access the fields and methods, which itself is enough to assert the required member constraint.

Extra information

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

Related suggestions: (put links to related suggestions here) #440 #641 #483

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 would be willing to help implement and/or test this
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.