Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Make Destructing Assignments in Match Statements First Class #611
Make Destructing Assignments in Match Statements First Class
When working with DUs, especially ones that describe a state machines
type StateMachine = | StateA of SomeType | StateB of OtherType | StateC of int let processStateA (s:StateMachine) = match s with | StateA v -> doSomethingWithAState v |> Success | _ -> Error "This State is not allowed" let processStateB (s:StateMachine) = match s with | StateB v -> BStateProcessing v |> Success | _ -> Error "This State is not allowed" let processStateC (s:StateMachine) = match s with | StateC v -> workingOnC v |> Success | _ -> Error "This State is not allowed"
Now most of this would be possible to extract and put into a generic method except for the destructing assignments.
let processStateGeneric doSomethingFn destructing (s:StateMachine) = match s with | destructing v -> doSomethingFn v |> Success | _ -> Error "This State is not allowed"
This would then allow then to define the functions to be redifined as this
let processStateA = processStateGeneric doSomethingWithAState StateA let processStateB = processStateGeneric BStateProcessing StateB let processStateC = processStateGeneric workingOnC StateC
The existing way of approaching this problem in F# is not available (Maybe some code quotations could do the trick)
Pros and Cons
The advantages of making this adjustment to F# are that handling of DUs (specifically of state machines) could be improved quite a bit.
The disadvantages of making this adjustment to F# are implementation woes, more complexity in the language (even thou I would assert that the contrary is true)
Estimated cost (XS, S, M, L, XL, XXL): L
Related suggestions: (put links to related suggestions here)
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
Just wanted to point out, that
The only thing which is not easily accessible right now is the partial active pattern for the union case name.
It would be cool to have a compiler support for those boiler plate function definitions :)
@mexx wow, I did not know that this is possible. knowing that I can put just a bit of code quotation magic and can probably write something like
let processStateA = processStateGeneric doSomethingWithAState (extract StateA) //extract: (SomeType -> StateMachine) -> (StateMachine -> SomeType)
If that works out I will close this issue
I have been able to condense this down to
type FooBar<'a, 'b> = | Foo of 'a | Bar of 'b let foo: FooBar<int, bool> = Foo 88 processGen Foo (fun x -> x + 1) foo // yields 89
The call to