Skip to content

Commit 952d4ba

Browse files
authored
correcting tutorial for Result.tryCreate (#266)
* corrected the examples in tryCreate.md * fixed Script.fsx * applying Error directly instead of piping
1 parent eb9f4d8 commit 952d4ba

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

gitbook/result/tryCreate.md

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,25 @@ type Longitude = private Longitude of float with
3131
if lng >= -180. && lng <= 180. then
3232
Ok (Longitude lng)
3333
else
34-
sprintf "%A is a invalid longitude value" lng |> Error
34+
Error $"%A{lng} is a invalid longitude value"
3535
```
3636

3737
Let's assume that we have few more similar types as below
3838

3939
```fsharp
40-
type Longitude = private Longitude of float with
40+
type Latitude = private Latitude of float with
4141
member this.Value =
42-
let (Longitude lng) = this
43-
lng
44-
static member TryCreate (lng : float) =
45-
if lng > -180. && lng < 180. then
46-
Ok (Longitude lng)
42+
let (Latitude lat) = this
43+
lat
44+
45+
// float -> Result<Latitude, string>
46+
static member TryCreate (lat : float) =
47+
if lat >= -90. && lat <= 90. then
48+
Ok (Latitude lat)
4749
else
48-
sprintf "%A is a invalid longitude value" lng |> Error
50+
Error $"%A{lat} is a invalid latitude value"
51+
52+
open System
4953
5054
type Tweet = private Tweet of string with
5155
member this.Value =
@@ -100,7 +104,8 @@ type CreatePostRequestDto = {
100104
We can then do result using `Result.tryResult` and the [`Result` infix operators](../result/operators.md) as below:
101105

102106
```fsharp
103-
open FsToolkit.ErrorHandling.Operator.Result
107+
open FsToolkit.ErrorHandling
108+
open FsToolkit.ErrorHandling.Operator.Validation
104109
105110
// CreatePostRequestDto -> Result<CreatePostRequest, (string * string) list>
106111
let validateCreatePostRequest (dto : CreatePostRequestDto) =
@@ -114,7 +119,7 @@ Here the types of the `Result.tryCreate` lines are inferred, and the types' `Try
114119

115120
```fsharp
116121
validateCreatePostRequest
117-
{Tweet = ""; Location = {Latitude = 300.; Longitude = 400.}};;
122+
{Tweet = ""; Location = {Latitude = 300.; Longitude = 400.}}
118123
// Error
119124
// [("latitude", "300.0 is a invalid latitude value")
120125
// ("longitude", "400.0 is a invalid longitude value")
@@ -124,7 +129,7 @@ validateCreatePostRequest
124129
These errors can then for example be returned in an API response:
125130

126131
```fsharp
127-
validateCreatePostRequest dto
132+
validateCreatePostRequest {Tweet = ""; Location = {Latitude = 300.; Longitude = 400.}}
128133
|> Result.mapError Map.ofList
129134
// Map<string, string>
130135
```
@@ -144,7 +149,7 @@ When serialized:
144149
In Example 1, we collected all the error messages. But what if we wanted to stop on the first error? One way to do this is to make use of the `result` computation expression instead of using infix operators from `Result` module.
145150

146151
```fsharp
147-
// CreatePostRequestDto -> Result<CreatePostRequest, string>
152+
// CreatePostRequestDto -> Result<CreatePostRequest, string * string>
148153
let validateCreatePostRequest (dto : CreatePostRequestDto) = result {
149154
let! t = Result.tryCreate "tweet" dto.Tweet
150155
let! lat = Result.tryCreate "latitude" dto.Location.Latitude
@@ -158,23 +163,26 @@ let validateCreatePostRequest (dto : CreatePostRequestDto) = result {
158163
In the examples above, we assume that a location is always required for creating a post. Let's assume that the requirement is changed and now the location is optional:
159164

160165
```fsharp
161-
type CreatePostRequest = {
162-
Tweet : Tweet
163-
Location : Location option
164-
}
165-
166166
type CreatePostRequestDto = {
167167
Tweet : string
168168
Location : LocationDto option
169169
}
170170
171+
type CreatePostRequest = {
172+
Tweet : Tweet
173+
Location : Location option
174+
}
175+
171176
let createPostRequest location tweet =
172177
{Tweet = tweet; Location = location}
173178
```
174179

175180
Then `validateCreatePostRequest` can be rewritten using the [Option.traverseResult](../option/traverseResult.md) function as below:
176181

177182
```fsharp
183+
open FsToolkit.ErrorHandling
184+
open FsToolkit.ErrorHandling.Operator.Validation
185+
178186
let validateLocation (dto : LocationDto) =
179187
location
180188
<!^> Result.tryCreate "latitude" dto.Latitude

src/FsToolkit.ErrorHandling/Script.fsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
open System
88
open FsToolkit.ErrorHandling
99
open FsToolkit.ErrorHandling.Operator.Validation
10-
open FsToolkit.ErrorHandling.CE.Result
1110

1211
type Tweet =
1312
private

0 commit comments

Comments
 (0)