Note that there are some explanatory texts on larger screens.

plurals
  1. POF# - weird behavior when pattern matching discriminated unions
    primarykey
    data
    text
    <p>I've been writing some F# now for about 6 months and I've come across some behavior that I can't explain. I have some boiled down code below. (value names have been changed to protect the innocent!)</p> <p>I have a hierarchy defined using record types rec1 and rec2, and also a dicriminated union type with possible values CaseA and CaseB. I'm calling a function ('mynewfunc') that takes a du_rec option type. Internally this function defines a recursive function that processes the hierarchy . </p> <p>I'm kicking off the processing by passing the None option value to represent the root of the hierarchy (In reality, this function is deserializing the hierarchy from a file).</p> <p>When I run the code below I hit the "failwith "invalid parent"" line of code. I can not understand why this is, because the None value that is passed down should match the outer pattern matching's None case. </p> <p>The code works if I delete either of the sets of comments. This is not a showstopper for me - I just feel a bit uncomfortable not knowing why this is happening (I thought I was understanding f#)</p> <p>Thanks in advance for any replies</p> <p>James</p> <pre><code>type rec2 = { name : string child : rec1 option } and rec1 = { name : string ; child : rec2 option } and du_rec = | Case1 of rec1 | Case2 of rec2 let mynewfunc (arg:du_rec option) = let rec funca (parent:du_rec option) = match parent with | Some(node) -&gt; match node with | Case2(nd) -&gt; printfn "hello" (* | Case1(nd) -&gt; printfn "bye bye" *) | _ -&gt; failwith "invalid parent" | None -&gt; // printfn "case3" () funcb( None ) and funcb (parent: du_rec option) = printfn "this made no difference" let node = funca(arg) () let rootAnnot = mynewfunc(None) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload