Skip to content

Commit fc2f5a3

Browse files
shethaaditAdit Shethpsfinaki
authored
Fix: Improve F# Recursive Module Example for Banana Type Logic (#44181)
* Fixed bug 41533. * Update docs/fsharp/language-reference/modules.md Comment resolution. Co-authored-by: Petr <[email protected]> --------- Co-authored-by: Adit Sheth <[email protected]> Co-authored-by: Petr <[email protected]>
1 parent d703cbd commit fc2f5a3

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

docs/fsharp/language-reference/modules.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,21 @@ module rec RecursiveModule =
9393
exception DontSqueezeTheBananaException of Banana
9494
9595
type Banana(orientation : Orientation) =
96-
member val IsPeeled = false with get, set
9796
member val Orientation = orientation with get, set
98-
member val Sides: PeelState list = [ Unpeeled; Unpeeled; Unpeeled; Unpeeled] with get, set
97+
member val Sides: PeelState list = [ Unpeeled; Unpeeled; Unpeeled; Unpeeled ] with get, set
9998
100-
member self.Peel() = BananaHelpers.peel self // Note the dependency on the BananaHelpers module.
101-
member self.SqueezeJuiceOut() = raise (DontSqueezeTheBananaException self) // This member depends on the exception above.
99+
member self.IsPeeled =
100+
self.Sides |> List.forall ((=) Peeled)
101+
102+
member self.Peel() =
103+
BananaHelpers.peel self
104+
|> fun peeledSides -> self.Sides <- peeledSides
105+
106+
member self.SqueezeJuiceOut() =
107+
raise (DontSqueezeTheBananaException self)
102108
103109
module BananaHelpers =
104-
let peel (b: Banana) =
110+
let peel (banana: Banana) =
105111
let flip (banana: Banana) =
106112
match banana.Orientation with
107113
| Up ->
@@ -115,9 +121,7 @@ module rec RecursiveModule =
115121
| Unpeeled -> Peeled
116122
| Peeled -> Peeled)
117123
118-
match b.Orientation with
119-
| Up -> b |> flip |> peelSides
120-
| Down -> b |> peelSides
124+
banana |> flip |> peelSides
121125
```
122126

123127
Note that the exception `DontSqueezeTheBananaException` and the class `Banana` both refer to each other. Additionally, the module `BananaHelpers` and the class `Banana` also refer to each other. This would not be possible to express in F# if you removed the `rec` keyword from the `RecursiveModule` module.

0 commit comments

Comments
 (0)