fslang-suggestions icon indicating copy to clipboard operation
fslang-suggestions copied to clipboard

Allow the record pattern to be implemented explicitly

Open baronfel opened this issue 9 years ago • 4 comments

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

baronfel avatar Oct 20 '16 01:10 baronfel

I've renamed this, see also #1152

dsyme avatar Jun 16 '22 16:06 dsyme

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

kerams avatar Nov 29 '22 20:11 kerams

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

dsyme avatar Apr 12 '23 13:04 dsyme

The type would have to implement the equality/comparison interfaces

I believe, since the syntax is relied upon visually, to assume those are implemented on record definition (unless some attributes are put to omit it), we should revise if potential warnings are worthy and can be enabled by default.

For example, when types with RecordSyntax are used with = operator, it should recommend to resort on Object.ReferenceEquals, but there would probably be other similar things to consider for the RFC.

If this is relevant, it should apply the same NoComparison and NoEquality adorned records.

smoothdeveloper avatar Jun 21 '25 02:06 smoothdeveloper