Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange behavior of F# records
    primarykey
    data
    text
    <p>There is a few cases when F# records behavior is strange to me:</p> <p><strong>No warning on ambiguity</strong></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><strong>Warning on records deconstruction instead of records construction</strong></p> <p>Instead of getting a warning on records construction in previous case, F# compiler issued a warning on records "deconstruction":</p> <pre><code>// Using Person and AnotherPerson types and "p" from the previous example! // We'll get a warning here: "The field labels and expected type of this // record expression or pattern do not uniquely determine a corresponding record type" let {Id = id; Name = name} = p </code></pre> <p>Note, that there is no warnings with pattern matching (I suspect thats because patterns are built using "records construction expressions" and not with "records deconstruction expression"):</p> <pre><code>match p with | {Id = _; Name = "Foo"} -&gt; printfn "case 1" | {Id = 42; Name = _} -&gt; printfn "case 2" | _ -&gt; printfn "case 3" </code></pre> <p><strong>Type inference error with missing field</strong></p> <p>F# compiler will choose second type and than will issue an error because Age field is missing!</p> <pre><code>type AnotherPerson = {Id: int; Name: string} type Person = {Id: int; Name: string; Age: int} // Error: "No assignment given for field 'Age' of type 'Person'" let p = {Id = 42; Name = "Foo";} </code></pre> <p><strong>Ugly syntax for "records deconstruction"</strong></p> <p>I asked several collegues of mine a question: "What this code is all about?"</p> <pre><code>type Person = {Id: int; Name: string;} let p = {Id = 42; Name = "Foo";} // What will happend here? let {Id = id; Name = name} = p </code></pre> <p>That was total surprise for everyone that "id" and "name" are actually "lvalues" although they placed at the "right hand side" of the the expression. I understand that this is much more about personal preferences, but for most people seems strange that in one particular case output values are placed at the right side of the expression.</p> <p>I don't think that all of this are bugs, I suspect that most of this stuff are actually features.<br> My question is: <strong>is there any rational behind such obscure behavior?</strong></p>
    singulars
    1. This table or related slice is empty.
    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.
 

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