Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Auto implement partial active recognizers for DU cases #766
Auto implement partial active recognizers for DU cases
I propose we add a way to auto-implement partial active recognizers for DU cases. This will provide a way to treat patterns as first class functions.
The existing way of approaching this problem in F# is to implement some functions by hand:
Pros and Cons
The advantages of making this adjustment to F# are: less typing, less code that adds nothing but noise that hides the intent in the end.
The disadvantages of making this adjustment to F# are that it's work (RFC, PR, testing).
Estimated cost (S):
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
The case exposed in #611 is a good example of how useful this is and how it relates to treat patterns as first class functions.
I also find this boiler-plate code while defining CODECs for discriminated unions to Json in Fleece by using alternatives:
And I'm sure that there might be more cases around.
The most obvious syntax I can think of is just the case enclosed in banana parens, ie
So it will be as if these functions:
were generated automatically after the type declaration, then the above code could be reduced to:
Another possibility is to use a different syntax, not related to partial active patterns, but the essence of this suggestion is not the syntax itself but the possibility to auto-generate those functions automatically.
This is a very interesting suggestion. We would have to consider compatibility here, likely only generating these functions if there isn't such an active recognizer name already in scope. But what if only a subset is in scope, do we still generate the remainders?
@cartermp good catch, I will update the breaking change box, as it will depend on the implementation.
I would say that either:
An alternative to creating many new methods and names:
trymatch du with CaseA x -> x tryfunction CaseA x -> x
match du with CaseA x -> Some x | _ -> None function CaseA x -> Some x | _ -> None
Pros: Doesn't create so many new methods and names. Deals with user-defined patterns, and more than one if needed. Fewer characters than current syntax.