Records With Non Unique Field Names
Last changed: -213.199.128.155

.

Check out the formatting tips on the right for help formatting and making links.

Use the template below:

Summary

F# record types are simple named types that support pattern matching and

construction using a field-assignment syntax:

 type MyRecord = { field1: int; field2: string }
 let myRecordValue = { field1 = 3; field2 = "3" }  

F# supports records in the same file having identical or overlapping sets of

field names. For example:

 type MyRecord1 = { field1: int; field2: string }
 type MyRecord2 = { field2: string; field3: int }

Ambiguity on access is resolved through type annotations via the same mechanism as that used for resolving .NET method and field names, i.e. the type of the expression must be known using the information available earlier in the file, where this information is processed left-to-right according to the standard Hindley-Milner inference algorithm. Sometimes an ExplicitTypeAnnotation will be required.

Question
What does "Sometimes an ExplicitTypeAnnotation will be required." mean? I would assume that it mean placing the type name after the indentifer, but in the below sample only thing3 compiles. It seems that if you do not use the ObjectExpressions syntax only the type define last will compile.
Answer
        type MyRecord1 = { field1: int; field2: string }
        type MyRecord2 = { field2: string; field3: int }


        let thing1 = { field1 = 1 ; field2 = "" }
        let (thing2 : MyRecord1) = { field1 = 1 ; field2 = "" }
        let thing3 = { field2 = "" ; field3 = 1 }

As of version 1.1 it is possible to resolve ambiguity on creation of records.

The syntax follows that of ObjectExpressions, which use "and" to separate the items. For example:

 { new MyRecord2 with field2 = 3 and field3 = "3" }  
 { new MyRecord1 with field1 = 2 and field3 = 4 }