Names bound to both the decomposed discriminated union components AND an original composed value

104 views Asked by At

I can't find documentation on this. I need to decompose a Discriminated Union value into some of its components but I also need to use the item as a whole within the body of a function as well.

I can do let matcher = function MyDU(_,b,_) -> b or let extractor MyDU(_,b,_) = b but what if I also need the reference to the MyDU value for something like ... -> RedundantWrapper(myDU, b)

I've tried:

let extractor myDU = 
    let MyDU(_,b,_) = myDU
    RedundantWrapper(myDU, b)

but I don't think that is the right syntax. Perhaps I can do this some longer way, but it seems like there would be a short way.

Thanks!

2

There are 2 answers

2
Daniel On BEST ANSWER

Like this:

let (Some(x) as o) = Some 1

// val o : int option = Some 1
// val x : int = 1
3
Jason Kleban On

Also, I just realized that this short-hand is allowed which I think everyone here will agree is PRETTY AWESOME.

let extractor (myDU & MyDU(_,b,_)) = 
    RedundantWrapper(myDU, b)