From acd40b4ee5327ea47e70fecf1e8af8501f819e1b Mon Sep 17 00:00:00 2001 From: Dave Lowe Date: Tue, 18 Dec 2018 21:17:03 +0000 Subject: [PATCH 1/7] Added Mutually Recursive Records example to records documentation --- docs/fsharp/language-reference/records.md | 36 +++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/docs/fsharp/language-reference/records.md b/docs/fsharp/language-reference/records.md index edf9490c2ae31..60d3d2c044df4 100644 --- a/docs/fsharp/language-reference/records.md +++ b/docs/fsharp/language-reference/records.md @@ -84,6 +84,42 @@ let defaultRecord2 = { Field1 = 1; Field2 = 25 } let rr3 = { defaultRecord1 with Field2 = 42 } ``` +## Creating Mutually Recursive Records + +Sometimes when creating a record, you may have the need to use a type which isn't defined and which you intend to create later. + +This can be achieved by using the `and` keyword to link 2 or more record types together by what is known as mutual recursion. + +The following records are created using the `and` keyword: + +```fsharp +// Create a Person type and use the Address type that is not defined +type Person = + { Name: string + Age: int + Address: Address } +// Define the Address type which is used in the Person record +and Address = + { Line1: string + Line2: string + PostCode: string } +``` + +The following records are not created with the `and` keyword and would not compile: + +```fsharp +// This will error as the Person record tries to use the Address type which is not defined using the `and` keyword +type Person = + { Name: string + Age: int + Address: Address } + +type Address = + { Line1: string + Line2: string + PostCode: string } +``` + ## Pattern Matching with Records Records can be used with pattern matching. You can specify some fields explicitly and provide variables for other fields that will be assigned when a match occurs. The following code example illustrates this. From c884e7d873cce7bc3feacf8ef8b1f8c0d8908b30 Mon Sep 17 00:00:00 2001 From: Phillip Carter Date: Wed, 19 Dec 2018 02:26:09 +0000 Subject: [PATCH 2/7] Update docs/fsharp/language-reference/records.md Co-Authored-By: iwasdavid --- docs/fsharp/language-reference/records.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/fsharp/language-reference/records.md b/docs/fsharp/language-reference/records.md index 60d3d2c044df4..5121fdf60d0ef 100644 --- a/docs/fsharp/language-reference/records.md +++ b/docs/fsharp/language-reference/records.md @@ -86,7 +86,7 @@ let rr3 = { defaultRecord1 with Field2 = 42 } ## Creating Mutually Recursive Records -Sometimes when creating a record, you may have the need to use a type which isn't defined and which you intend to create later. + Sometime when creating a record, you may want to have it depend on another type that you would like to define afterwards. This is a compile error unless you define the record types to be mutually recursive. This can be achieved by using the `and` keyword to link 2 or more record types together by what is known as mutual recursion. @@ -158,4 +158,4 @@ If you need reference equality for records, add the attribute `[ Date: Wed, 19 Dec 2018 02:27:04 +0000 Subject: [PATCH 3/7] Update docs/fsharp/language-reference/records.md Co-Authored-By: iwasdavid --- docs/fsharp/language-reference/records.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fsharp/language-reference/records.md b/docs/fsharp/language-reference/records.md index 5121fdf60d0ef..91bd4aaf84cc0 100644 --- a/docs/fsharp/language-reference/records.md +++ b/docs/fsharp/language-reference/records.md @@ -88,7 +88,7 @@ let rr3 = { defaultRecord1 with Field2 = 42 } Sometime when creating a record, you may want to have it depend on another type that you would like to define afterwards. This is a compile error unless you define the record types to be mutually recursive. -This can be achieved by using the `and` keyword to link 2 or more record types together by what is known as mutual recursion. + Defining mutually recursive records is done with the `and` keyword. This lets you link 2 or more record types together. The following records are created using the `and` keyword: From 088b7d8f42d71ff85816d98f5d925f2281dff9af Mon Sep 17 00:00:00 2001 From: Phillip Carter Date: Wed, 19 Dec 2018 02:27:23 +0000 Subject: [PATCH 4/7] Update docs/fsharp/language-reference/records.md Co-Authored-By: iwasdavid --- docs/fsharp/language-reference/records.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fsharp/language-reference/records.md b/docs/fsharp/language-reference/records.md index 91bd4aaf84cc0..903f2f6f411df 100644 --- a/docs/fsharp/language-reference/records.md +++ b/docs/fsharp/language-reference/records.md @@ -90,7 +90,7 @@ let rr3 = { defaultRecord1 with Field2 = 42 } Defining mutually recursive records is done with the `and` keyword. This lets you link 2 or more record types together. -The following records are created using the `and` keyword: +For example, the following code defines a `Person` and `Address` type as mutually recursive: ```fsharp // Create a Person type and use the Address type that is not defined From 0c2af41e47f0d16f04bfade03c9f007a0ff2da9e Mon Sep 17 00:00:00 2001 From: Phillip Carter Date: Wed, 19 Dec 2018 02:27:51 +0000 Subject: [PATCH 5/7] Update docs/fsharp/language-reference/records.md Co-Authored-By: iwasdavid --- docs/fsharp/language-reference/records.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fsharp/language-reference/records.md b/docs/fsharp/language-reference/records.md index 903f2f6f411df..f17bf483575dc 100644 --- a/docs/fsharp/language-reference/records.md +++ b/docs/fsharp/language-reference/records.md @@ -105,7 +105,7 @@ and Address = PostCode: string } ``` -The following records are not created with the `and` keyword and would not compile: +If you were to define the previous example without the `and` keyword, then it would not compile. The `and` keyword is required for mutually recursive definitions. ```fsharp // This will error as the Person record tries to use the Address type which is not defined using the `and` keyword From 17383adef33924aa9ec99d0b9f2ed96f82800103 Mon Sep 17 00:00:00 2001 From: Dave Lowe Date: Wed, 19 Dec 2018 06:51:46 +0000 Subject: [PATCH 6/7] Removed bad code example that wouldn't compile --- docs/fsharp/language-reference/records.md | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/docs/fsharp/language-reference/records.md b/docs/fsharp/language-reference/records.md index f17bf483575dc..3dbc78690eccc 100644 --- a/docs/fsharp/language-reference/records.md +++ b/docs/fsharp/language-reference/records.md @@ -107,19 +107,6 @@ and Address = If you were to define the previous example without the `and` keyword, then it would not compile. The `and` keyword is required for mutually recursive definitions. -```fsharp -// This will error as the Person record tries to use the Address type which is not defined using the `and` keyword -type Person = - { Name: string - Age: int - Address: Address } - -type Address = - { Line1: string - Line2: string - PostCode: string } -``` - ## Pattern Matching with Records Records can be used with pattern matching. You can specify some fields explicitly and provide variables for other fields that will be assigned when a match occurs. The following code example illustrates this. From fa18f38d457a0ebb71baf8b2917ec22467a4be2e Mon Sep 17 00:00:00 2001 From: Dave Lowe Date: Wed, 19 Dec 2018 06:55:33 +0000 Subject: [PATCH 7/7] Removed white space --- docs/fsharp/language-reference/records.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/fsharp/language-reference/records.md b/docs/fsharp/language-reference/records.md index 3dbc78690eccc..4c17acfe23c16 100644 --- a/docs/fsharp/language-reference/records.md +++ b/docs/fsharp/language-reference/records.md @@ -86,9 +86,9 @@ let rr3 = { defaultRecord1 with Field2 = 42 } ## Creating Mutually Recursive Records - Sometime when creating a record, you may want to have it depend on another type that you would like to define afterwards. This is a compile error unless you define the record types to be mutually recursive. +Sometime when creating a record, you may want to have it depend on another type that you would like to define afterwards. This is a compile error unless you define the record types to be mutually recursive. - Defining mutually recursive records is done with the `and` keyword. This lets you link 2 or more record types together. +Defining mutually recursive records is done with the `and` keyword. This lets you link 2 or more record types together. For example, the following code defines a `Person` and `Address` type as mutually recursive: