F #: Destroying discriminatory association binding

open System let x = (1, 2) let (p, q) = x printfn "A %A" x printfn "B %A %A" pq let y = Some(1, 2) try let None = y () with | ex -> printfn "C %A" ex let Some(r, s) = y printfn "D %A" y // printfn "E %A %A" rs 

When I uncomment the last line, the compiler rejects the code complaining

/home/rRiy1O/prog.fs(16.19): error FS0039: value or constructor 'r' not defined
/home/rRiy1O/prog.fs(16,21): error FS0039: value or constructor 'not defined

Is it possible to use enums in the destruction of let ?

But firstly, even when I comment on the last line ... what am I doing here? Here's the conclusion:

 A (1, 2) B 1 2 D Some (1, 2) 

Update

For the record here is a fixed version:

 open System let x = (1, 2) let (p, q) = x printfn "A %A" x printfn "B %A %A" pq let y = Some(1, 2) try let (None) = y () with | ex -> printfn "C %A" ex let (Some(r, s)) = y printfn "D %A" y printfn "E %A %A" rs 

Output:

 A (1, 2) B 1 2 C MatchFailureException ("/home/MBO542/prog.fs",10,6) D Some (1, 2) E 1 2 

Perfect.

+6
source share
1 answer

How are you trying to destroy y :

 let Some(r, s) = y 

In fact, you define a function called Some , with two arguments, r and s , passed in the form on the form.

For proper destructuring you need to add parentheses:

 let (Some (r, s)) = y 

By the way, the same thing happens inside the try block: the string let None = y creates a new value called None and is equal to y .

+6
source

All Articles