Closed IsaacSchemm closed 4 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?
Marked as approved in principle!
@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.
This is my favorite feature since C# added async and await. I would love to see it appear in F# ASAP
@xperiandri are you interested in writing an RFC for this and/or a PR to further the proposal?
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.
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 aDictionary
.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 aCollectionBuilderAttribute
to the type and implementing a static creation method.This code can be used today in C#:
With this change, the code could become:
The existing ways of approaching this problem are to:
System.Collections.Immutable
; these types do not implement value equality; so another approach must be used to compare two objectsListModule.OfSeq
orListModule.OfArray
object.Equals
and withIEquatable<T>
) and collection expressions, as well as whatever other functions / features are neededPros 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:
1086
969
Affidavit (please submit!)
Please tick these items by placing a cross in the box:
Please tick all that apply: