fsharp / fslang-suggestions

The place to make suggestions, discuss and vote on F# language and core library features
346 stars 21 forks source link

Support for C# collection expressions in F# lists and sets #1355

Closed IsaacSchemm closed 4 months ago

IsaacSchemm commented 8 months ago

I propose we add support for C# collection expressions to F# lists and sets, so that a collection of this type can be initialized from C# code with a collection expression.

The use case for F# lists and sets in C# code, as I see it, is to extend the immutability and value equality of C# records to those with collections of equatable items (strings, integers, other records) as properties/fields. By combining a record with an immutable collection type that (like the record) implements value equality, a complex type can be constructed that is fully immutable and supports "deep" equality checks all the way down, as is often done in F#. This becomes very useful when writing unit tests that need to define and check all fields and sub-fields on objects,. It also opens up use cases for these complex types to be used in a HashSet or as keys in a Dictionary.

C# collection expressions don't have "automatic" support for immutable collections (the compiler looks for an Add method), but support can be added to a collection type by adding a CollectionBuilderAttribute to the type and implementing a static creation method.

This code can be used today in C#:

record Poem {
    public string Name { get; init; } = "Untitled";
    public string Content { get; init; } = "";
    public FSharpSet<string> Tags { get; init; } = SetModule.Empty<string>();
}

Poem originalPoem = repository.GetPoem(id);
Poem newPoem = originalPoem with { Tags = SetModule.FromArray(["sonnet", "moon", "sky"]) };
if (originalPoem != newPoem) {
    repository.UpdatePoem(id, newPoem);
}

With this change, the code could become:

record Poem {
    public string Name { get; init; } = "Untitled";
    public string Content { get; init; } = "";
    public FSharpSet<string> Tags { get; init; } = [];
}

Poem originalPoem = repository.GetPoem(id);
Poem newPoem = originalPoem with { Tags = ["sonnet", "moon", "sky"] };
if (originalPoem != newPoem) {
    repository.UpdatePoem(id, newPoem);
}

The existing ways of approaching this problem are to:

Pros and Cons

The advantages of making this adjustment to F# are:

The disadvantages of making this adjustment to F# are:

Extra information

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

Related suggestions:

Affidavit (please submit!)

Please tick these items by placing a cross in the box:

Please tick all that apply:

Happypig375 commented 8 months ago

Do you mean F# should define these methods for C# consumption? Should this relate to https://github.com/fsharp/fslang-suggestions/issues/969 for usage in F# too?

dsyme commented 8 months ago

Marked as approved in principle!

IsaacSchemm commented 8 months ago

@Happypig375 I'll add #969 as related - it looks like if F# adds the ability to consume CollectionBuilderAttribute, it would be tracked as part of that issue. Right now this would just be for C# consumption.

xperiandri commented 6 months ago

This is my favorite feature since C# added async and await. I would love to see it appear in F# ASAP

abelbraaksma commented 6 months ago

@xperiandri are you interested in writing an RFC for this and/or a PR to further the proposal?

vzarytovskii commented 6 months ago

To make sure everyone understands what it covers. It doesn't cover having "one syntax" for all collections in F#, but make C# "understand" out collections.

brianrourkeboll commented 5 months ago