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 the record pattern to be implemented explicitly #164

Open
baronfel opened this issue Oct 20, 2016 · 3 comments
Open

Allow the record pattern to be implemented explicitly #164

baronfel opened this issue Oct 20, 2016 · 3 comments

Comments

@baronfel
Copy link
Contributor

baronfel commented Oct 20, 2016

Submitted by Don Syme on 3/21/2014 12:00:00 AM
25 votes on UserVoice prior to migration

People sometimes find it hard to transition from records to class types - something which comes up when seeking to encapsulate some of the details of the record type. One particular reason for this is because their codebase may uses copy-and-update on record types. One approach to easing the transition would be to support copy-and-update on class types, as long as the class type follows a particular design pattern.

One pattern-based approach could permit both normal record syntax and copy-and-update syntax, e.g.

[<RecordSyntax>]
type R(a:int, b:int) =
    member x.A = a
    member x.B = b
    member x.C = f(a,b)

{ a = e1; b = e2 } --> R(a=e1,b=e2}
{ r with a = e1} --> R(a=e1,b=r.B)

Whether there were one or two attributes (one for 'RecordSyntax' and one for 'CopyAndUpdateSyntax') would be up for discussion. Presumably using either attribute would give result in a declaration-time check that members exist to match constructor arguments. Matching uppercase properties to lowercase argument names is somewhat inelegant but in the balance is likely to be a reasonable price to pay for following .NET and F# design norms.

Original UserVoice Submission
Archived Uservoice Comments

@dsyme dsyme removed the open label Oct 29, 2016
@dsyme dsyme changed the title Copy-and-update on class types and on records of different types. Implementing the record pattern Jun 16, 2022
@dsyme dsyme changed the title Implementing the record pattern Allow the record pattern to be implemented explicitly Jun 16, 2022
@dsyme
Copy link
Collaborator

dsyme commented Jun 16, 2022

I've renamed this, see also #1152

@kerams
Copy link

kerams commented Nov 29, 2022

Would the attribute imply structural equality, would such a class otherwise be indistinguishable from a bona fide record?

@dsyme
Copy link
Collaborator

dsyme commented Apr 12, 2023

Would the attribute imply structural equality, would such a class otherwise be indistinguishable from a bona fide record?

The type would have to implement the equality/comparison interfaces

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

3 participants