You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/fsharp/language-reference/modules.md
+12-8Lines changed: 12 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -93,15 +93,21 @@ module rec RecursiveModule =
93
93
exception DontSqueezeTheBananaException of Banana
94
94
95
95
type Banana(orientation : Orientation) =
96
-
member val IsPeeled = false with get, set
97
96
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
99
98
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)
102
108
103
109
module BananaHelpers =
104
-
let peel (b: Banana) =
110
+
let peel (banana: Banana) =
105
111
let flip (banana: Banana) =
106
112
match banana.Orientation with
107
113
| Up ->
@@ -115,9 +121,7 @@ module rec RecursiveModule =
115
121
| Unpeeled -> Peeled
116
122
| Peeled -> Peeled)
117
123
118
-
match b.Orientation with
119
-
| Up -> b |> flip |> peelSides
120
-
| Down -> b |> peelSides
124
+
banana |> flip |> peelSides
121
125
```
122
126
123
127
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