fsharp / fsharp/fslang-suggestions

Allow the record pattern to be implemented explicitly

Open
#164 4 comments 7 reactions 0 assignees View on GitHub
approved-in-principle area: records
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

**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.

``` fsharp
[]
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](https://fslang.uservoice.com/forums/245727-f-language/suggestions/5663704)**
[Archived Uservoice Comments](../tree/master/archive/suggestion-5663704-copy-and-update-on-class-types-and-on-records-of.md#comments)

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the proposed record and copy-and-update syntax in this issue, then read the archived UserVoice comments at archive/suggestion-5663704-copy-and-update-on-class-types-and-on-records-of.md. A contribution would first need a settled design for the attribute model, syntax, and declaration-time checks; no implementation files or tests are identified here.

Written by the indexing model from the issue text.

Assessment

Tech stack
fsharp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.