Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
Allow variable patterns for tuples in Discriminated Union matching #743
Allow variable patterns in Discriminated Union matching
I propose we allow variable patterns in Discriminated Union matching
The existing way of approaching this problem in F# is explicitly matching all tuple elements.
type DU = DUCase of int * int * int match du with | DUCase tuple -> f tuple
type DU = DUCase of int * int * int match du with | DUCase (a, b, c) -> f (a, b, c)
Pros and Cons
The advantage of making this adjustment to F# is shorter and concise matching.
The disadvantage of making this adjustment to F# is implementation effort required.
Estimated cost (XS, S, M, L, XL, XXL): XS~S
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
I don't know what's reason why this was not allowed from the beginning, I wonder whether this is a bug.
I agree this would be good to have. Some subtleties:
type DUTuple = DUTupleCase of (int * int) function | DUTupleCase data -> ...
type DUB = | DUBCase1 of int * int | DUBCase2 of int * b:int function | DUBCase1 case1Data -> ... | DUBCase2 case2Data -> ...
What type should
The natural type for
The other option is to strip the names and match as tuples. So
@charlesroddie this would be interesting if there were named tuples (i.e. C#'s named ValueTuples); then it would be a 1-to-1 mapping.
Pros: allows this match rule to work
Just a thought.
Barring either of those, I'm fine with it binding to a plain old tuple. That would not be unexpected to me.
The asymmetry between:
Is confusing, since both are currently tuples. If