fsharp / fsharp/fslang-suggestions
Add `Deconstruct` to Discriminated Unions codegen for smoother C# consumption
- Dominant language
- No language data
- Stars
- 373
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
I propose we add a C# convention Deconstruct method to Discriminated Unions. So that if I make a DU in F#
```fsharp
type Suit = Heart | Diamond | Club | Spade
type Card = {Rank:int; Suit:Suit}
type Hand = Card seq
type ScorableHand = EntireHand of Hand
| SplitHand of Hand * Hand
| TwoHand of Hand * Hand * Card
| NoHand of Hand
```
I can consume it with the deconstructing pattern matching syntax in C# like this:
```csharp
static ScoringList ScoreValue(ScorableHand s) =>
s switch {
ScorableHand.EntireHand (var h) =>
Seq.ToList(new [] {RankValue(h)}),
ScorableHand.SplitHand(var h, var k) =>
Seq.ToList(new [] {RankValue(h), RankValue(k)}),
ScorableHand.TwoHand (var bp, var sp,var k) =>
Seq.ToList(new [] {RankValue(bp), RankValue(sp), Rank(k)}),
ScorableHand.NoHand (var h) => Seq.ToList(Ranking(h)),
_ => throw new InvalidOperationException()
};
```
Instead of the existing way with `{Item#: var}`:
```csharp
static ScoringList ScoreValue(ScorableHand s) =>
s switch {
ScorableHand.EntireHand { Item: var h} =>
Seq.ToList(new [] {RankValue(h)}),
ScorableHand.SplitHand { Item1: var h, Item2: var k} =>
Seq.ToList(new [] {RankValue(h), RankValue(k)}),
ScorableHand.TwoHand {Item1: var bp, Item2: var sp, Item3: var k} =>
Seq.ToList(new [] {RankValue(bp), RankValue(sp), Rank(k)}),
ScorableHand.NoHand {Item: var h} => Seq.ToList(Ranking(h)),
_ => throw new InvalidOperationException()
};
```
## Pros and Cons
The advantages of making this adjustment to F# are that it would make it smoother for F# Discriminated Unions to be consumed from C#. And C# doesn't have their own Discriminated Unions.
The disadvantages of making this adjustment to F# would be if F# ever wanted to support the Deconstruct feature from C# it might cause complications for such an implementation.
## Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
C# lets you make Deconstruct methods by extension methods. So I was able to test this having this in a working sample.
https://github.com/jbtule/ConsumeFSharp-FromCSharp/pull/1
## 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
* [ ] I or my company would be willing to help implement and/or test this
## For Readers
If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.