DavidArno / SuccincT

Discriminated unions, pattern matching and partial applications for C#
MIT License
268 stars 15 forks source link

Tuple pattern matching is a bit rubbish and needs improving to handle C#7+ value tuples #41

Closed DavidArno closed 7 years ago

DavidArno commented 7 years ago

Currently, pattern matching on eg (1, 2) just uses a Matcher<T1, T2, TResult> matcher, forcing the tuple to be decomposed and supplying the two elements as separate parameters to Where and Do.

With C# 7.1, and its Inferred tuple names feature this becomes even more of an issue as it ought to be possible to do, eg:

(a:1, b:1).Match().To<bool>
          .Where(t => t.a < 0).Do(...

The pattern matching of value tuples needs a rewrite to use a tuple-specific pattern matcher to enable such functionality.

DavidArno commented 7 years ago

Unfortunately, it turns out that this change isn't possible with C# 7.1. Whilst the example in the OP would work, something like:

(a:1, b:1).Match().To<bool>
          .With(1, __).Do(t => t.a ...

won't work. The type returned by Match has to be T, not (T1, T2). Using the latter (to enable wildcard support and to constrain the matcher to tuples) means that the item names are still lost, as they are for all tuple expressions in C# 7.

See this comment on the C# language repo for details.

Closing this issue therefore as there's no real solution to this yet.