Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your examples can be divided into two categories: <strong>record expressions</strong> and <strong>record patterns</strong>. While record expressions <em>require to declare all fields</em> and return some expressions, record patterns have <em>optional fields</em> and are for the purpose of pattern matching. <a href="http://msdn.microsoft.com/en-us/library/dd233184.aspx" rel="noreferrer">The MSDN page on Records</a> have two clear sections on them, it may worth a read.</p> <p>In this example,</p> <pre><code>type AnotherPerson = {Id: int; Name: string} type Person = {Id: int; Name: string;} // F# compiler will use second type without any complains or warnings let p = {Id = 42; Name = "Foo";} </code></pre> <p>the behaviour is clear from the rule stated in <a href="http://msdn.microsoft.com/en-us/library/dd233184.aspx" rel="noreferrer">the MSDN page above</a>.</p> <blockquote> <p>The labels of the most recently declared type take precedence over those of the previously declared type</p> </blockquote> <p>In the case of pattern matching, you focus on creating some bindings you need. So you can write </p> <pre><code>type Person = {Id: int; Name: string;} let {Id = id} = p </code></pre> <p>in order to get <code>id</code> binding for later use. Pattern matching on let bindings may look a bit weird, but it's very similar to the way you usually pattern match in function parameters:</p> <pre><code>type Person = {Id: int; Name: string;} let extractName {Name = name} = name </code></pre> <p>I think warnings on your pattern matching examples are justified because the compiler can't guess your intention.</p> <p>Nevertheless, different records with duplicate fields are not recommended. At least you should use qualified names to avoid confusion:</p> <pre><code>type AnotherPerson = {Id: int; Name: string} type Person = {Id: int; Name: string; Age: int} let p = {AnotherPerson.Id = 42; Name = "Foo"} </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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