fsharp / fsharp/fslang-suggestions

Provide a way for C#/.NET to use records' copy-and-update functionality

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

Description

# Provide a way for C#/.NET to use records' copy-and-update functionality

There should be a way to use F# records' copy-and-update feature from C#. If you have a record with a lot of fields, it's pretty trivial to copy the whole thing and just update a single value, as in:

```fsharp
{ myRecord with field10 = "foo" }
```

Whereas if you're using this record in C#, doing the same thing is so painful:

```csharp
var myRecord = ...;
var myRecord2 = new MyRecord(myRecord.field1, myRecord.field2, ... , "foo")
```

As exemplified in [this StackOverflow answer](https://stackoverflow.com/a/18152339/1231925), you can certainly define your own `With` method with optional parameters that performs a copy-and-update, but it's only really worth it once you do this to a particular record more than one time.

I propose that F# should emit this method by default on records, so that a C# caller can have the benefit. For example, given the record:

```fsharp
type Address = { line1: string; city: string; state: StateCode; zipCode: string; country: CountryCode }
```

F# would now add a method that looks like this from C#:

```csharp
public class Address {
// Properties here
public Address With(line1: string = null, city: string = null, state: StateCode = null, zipCode: string = null, country: CountryCode = null) {
new Address(line1 ?? this.line1, city ?? this.city, stateCode ?? this.stateCode, zipCode ?? this.zipCode, countryCode ?? this.countryCode)
}
}
```

Alternatively, that could be a static method instead of an instance method, as written in the StackOverflow answer.

An ugly workaround is to make the record mutable, to follow C#'s pattern of object initializers, but this is terrible and can only be used if you control the record definition.

## Pros and Cons

Pros:
* This would improve the interop story with F# and other .NET languages.

Cons:
* This increases the amount of generated code for every record (contributing to executable bloat)
* It's work

## Extra information

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

Related suggestions: (put links to related suggestions here)

## Affidavit (please submit!)

Please tick this by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [x] 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:
* [x] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.