strangelights.com

Main F# Site

Edit Edit
Print Print
Recent Changes Recent Changes
Subscriptions Subscriptions
Lost and Found Lost and Found
Find References Find References
Rename Rename
Search
List all versions List all versions
Records With Non Unique Field Names
.

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 }
Welcome to F Sharp Wiki, view the HomePage

This site supports the new NoFollow anti-spam initiative.

Recent Topics

Copyright 2005, Robert Pickering (Others where credited) | Terms of Use