Skip to content

Commit 047d472

Browse files
authored
Merge pull request #1350 from wallymathieu/RFC-FS-1004-Result-methods
Adding uncontentious methods from RFC FS-1004
2 parents 8179097 + 11b791b commit 047d472

File tree

13 files changed

+159
-4
lines changed

13 files changed

+159
-4
lines changed

src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Core/ResultTests.fs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ type EmailValidation=
1313
| Empty
1414
| NoAt
1515

16-
module Results=
17-
let bind f m = match m with Error e -> Error e | Ok x -> f x
18-
19-
open Results
16+
open Result
2017

2118
[<TestFixture>]
2219
type ResultTests() =
@@ -35,10 +32,54 @@ type ResultTests() =
3532
let actual = validate_email email
3633
Assert.AreEqual(expected, actual)
3734

35+
let toUpper (v:string) = v.ToUpper()
36+
37+
let shouldBeOkWithValue expected maybeOk = match maybeOk with | Error e-> failwith "Expected Ok, got Error!" | Ok v->Assert.AreEqual(expected, v)
38+
39+
let shouldBeErrorWithValue expected maybeError = match maybeError with | Error e-> Assert.AreEqual(expected, e) | Ok v-> failwith "Expected Error, got Ok!"
40+
41+
let addOneOk (v:int) = Ok (v+1)
3842

3943
[<Test>]
4044
member this.CanChainTogetherSuccessiveValidations() =
4145
test_validate_email "" (Error Empty)
4246
test_validate_email "something_else" (Error NoAt)
4347
test_validate_email "some@email.com" (Ok "some@email.com")
4448

49+
[<Test>]
50+
member this.MapWillTransformOkValues() =
51+
Ok "some@email.com"
52+
|> map toUpper
53+
|> shouldBeOkWithValue "SOME@EMAIL.COM"
54+
55+
[<Test>]
56+
member this.MapWillNotTransformErrorValues() =
57+
Error "my error"
58+
|> map toUpper
59+
|> shouldBeErrorWithValue "my error"
60+
61+
[<Test>]
62+
member this.MapErrorWillTransformErrorValues() =
63+
Error "my error"
64+
|> mapError toUpper
65+
|> shouldBeErrorWithValue "MY ERROR"
66+
67+
[<Test>]
68+
member this.MapErrorWillNotTransformOkValues() =
69+
Ok "some@email.com"
70+
|> mapError toUpper
71+
|> shouldBeOkWithValue "some@email.com"
72+
73+
[<Test>]
74+
member this.BindShouldModifyOkValue() =
75+
Ok 42
76+
|> bind addOneOk
77+
|> shouldBeOkWithValue 43
78+
79+
[<Test>]
80+
member this.BindErrorShouldNotModifyError() =
81+
Error "Error"
82+
|> bind addOneOk
83+
|> shouldBeErrorWithValue "Error"
84+
85+

src/fsharp/FSharp.Core.Unittests/SurfaceArea.Silverlight.2.0.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,13 @@ Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: Int32 GetHashCode(
24462446
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.String ToString()
24472447
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.Type GetType()
24482448
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: Void .ctor()
2449+
Microsoft.FSharp.Core.ResultModule: Boolean Equals(System.Object)
2450+
Microsoft.FSharp.Core.ResultModule: Int32 GetHashCode()
2451+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[T,TResult] MapError[TError,TResult,T](Microsoft.FSharp.Core.FSharpFunc`2[TError,TResult], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2452+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[TResult,TError] Bind[T,TResult,TError](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpResult`2[TResult,TError]], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2453+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[TResult,TError] Map[T,TResult,TError](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2454+
Microsoft.FSharp.Core.ResultModule: System.String ToString()
2455+
Microsoft.FSharp.Core.ResultModule: System.Type GetType()
24492456
Microsoft.FSharp.Core.SealedAttribute: Boolean Equals(System.Object)
24502457
Microsoft.FSharp.Core.SealedAttribute: Boolean Match(System.Object)
24512458
Microsoft.FSharp.Core.SealedAttribute: Boolean Value

src/fsharp/FSharp.Core.Unittests/SurfaceArea.coreclr.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,6 +2789,13 @@ Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.Object get_
27892789
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.String ToString()
27902790
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.Type GetType()
27912791
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: Void .ctor()
2792+
Microsoft.FSharp.Core.ResultModule: Boolean Equals(System.Object)
2793+
Microsoft.FSharp.Core.ResultModule: Int32 GetHashCode()
2794+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[T,TResult] MapError[TError,TResult,T](Microsoft.FSharp.Core.FSharpFunc`2[TError,TResult], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2795+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[TResult,TError] Bind[T,TResult,TError](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpResult`2[TResult,TError]], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2796+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[TResult,TError] Map[T,TResult,TError](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2797+
Microsoft.FSharp.Core.ResultModule: System.String ToString()
2798+
Microsoft.FSharp.Core.ResultModule: System.Type GetType()
27922799
Microsoft.FSharp.Core.SealedAttribute: Boolean Equals(System.Object)
27932800
Microsoft.FSharp.Core.SealedAttribute: Boolean IsDefaultAttribute()
27942801
Microsoft.FSharp.Core.SealedAttribute: Boolean Match(System.Object)

src/fsharp/FSharp.Core.Unittests/SurfaceArea.net20.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2721,6 +2721,13 @@ Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.Object get_
27212721
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.String ToString()
27222722
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.Type GetType()
27232723
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: Void .ctor()
2724+
Microsoft.FSharp.Core.ResultModule: Boolean Equals(System.Object)
2725+
Microsoft.FSharp.Core.ResultModule: Int32 GetHashCode()
2726+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[T,TResult] MapError[TError,TResult,T](Microsoft.FSharp.Core.FSharpFunc`2[TError,TResult], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2727+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[TResult,TError] Bind[T,TResult,TError](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpResult`2[TResult,TError]], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2728+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[TResult,TError] Map[T,TResult,TError](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2729+
Microsoft.FSharp.Core.ResultModule: System.String ToString()
2730+
Microsoft.FSharp.Core.ResultModule: System.Type GetType()
27242731
Microsoft.FSharp.Core.SealedAttribute: Boolean Equals(System.Object)
27252732
Microsoft.FSharp.Core.SealedAttribute: Boolean IsDefaultAttribute()
27262733
Microsoft.FSharp.Core.SealedAttribute: Boolean Match(System.Object)

src/fsharp/FSharp.Core.Unittests/SurfaceArea.net40.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2889,6 +2889,13 @@ Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.Object get_
28892889
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.String ToString()
28902890
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.Type GetType()
28912891
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: Void .ctor()
2892+
Microsoft.FSharp.Core.ResultModule: Boolean Equals(System.Object)
2893+
Microsoft.FSharp.Core.ResultModule: Int32 GetHashCode()
2894+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[T,TResult] MapError[TError,TResult,T](Microsoft.FSharp.Core.FSharpFunc`2[TError,TResult], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2895+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[TResult,TError] Bind[T,TResult,TError](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpResult`2[TResult,TError]], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2896+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[TResult,TError] Map[T,TResult,TError](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2897+
Microsoft.FSharp.Core.ResultModule: System.String ToString()
2898+
Microsoft.FSharp.Core.ResultModule: System.Type GetType()
28922899
Microsoft.FSharp.Core.SealedAttribute: Boolean Equals(System.Object)
28932900
Microsoft.FSharp.Core.SealedAttribute: Boolean IsDefaultAttribute()
28942901
Microsoft.FSharp.Core.SealedAttribute: Boolean Match(System.Object)

src/fsharp/FSharp.Core.Unittests/SurfaceArea.portable259.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2856,6 +2856,13 @@ Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.Object get_
28562856
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.String ToString()
28572857
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.Type GetType()
28582858
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: Void .ctor()
2859+
Microsoft.FSharp.Core.ResultModule: Boolean Equals(System.Object)
2860+
Microsoft.FSharp.Core.ResultModule: Int32 GetHashCode()
2861+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[T,TResult] MapError[TError,TResult,T](Microsoft.FSharp.Core.FSharpFunc`2[TError,TResult], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2862+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[TResult,TError] Bind[T,TResult,TError](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpResult`2[TResult,TError]], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2863+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[TResult,TError] Map[T,TResult,TError](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2864+
Microsoft.FSharp.Core.ResultModule: System.String ToString()
2865+
Microsoft.FSharp.Core.ResultModule: System.Type GetType()
28592866
Microsoft.FSharp.Core.SealedAttribute: Boolean Equals(System.Object)
28602867
Microsoft.FSharp.Core.SealedAttribute: Boolean IsDefaultAttribute()
28612868
Microsoft.FSharp.Core.SealedAttribute: Boolean Match(System.Object)

src/fsharp/FSharp.Core.Unittests/SurfaceArea.portable47.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,6 +2857,13 @@ Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.Object get_
28572857
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.String ToString()
28582858
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.Type GetType()
28592859
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: Void .ctor()
2860+
Microsoft.FSharp.Core.ResultModule: Boolean Equals(System.Object)
2861+
Microsoft.FSharp.Core.ResultModule: Int32 GetHashCode()
2862+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[T,TResult] MapError[TError,TResult,T](Microsoft.FSharp.Core.FSharpFunc`2[TError,TResult], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2863+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[TResult,TError] Bind[T,TResult,TError](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpResult`2[TResult,TError]], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2864+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[TResult,TError] Map[T,TResult,TError](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2865+
Microsoft.FSharp.Core.ResultModule: System.String ToString()
2866+
Microsoft.FSharp.Core.ResultModule: System.Type GetType()
28602867
Microsoft.FSharp.Core.SealedAttribute: Boolean Equals(System.Object)
28612868
Microsoft.FSharp.Core.SealedAttribute: Boolean IsDefaultAttribute()
28622869
Microsoft.FSharp.Core.SealedAttribute: Boolean Match(System.Object)

src/fsharp/FSharp.Core.Unittests/SurfaceArea.portable7.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2869,6 +2869,13 @@ Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.Object get_
28692869
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.String ToString()
28702870
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.Type GetType()
28712871
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: Void .ctor()
2872+
Microsoft.FSharp.Core.ResultModule: Boolean Equals(System.Object)
2873+
Microsoft.FSharp.Core.ResultModule: Int32 GetHashCode()
2874+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[T,TResult] MapError[TError,TResult,T](Microsoft.FSharp.Core.FSharpFunc`2[TError,TResult], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2875+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[TResult,TError] Bind[T,TResult,TError](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpResult`2[TResult,TError]], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2876+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[TResult,TError] Map[T,TResult,TError](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2877+
Microsoft.FSharp.Core.ResultModule: System.String ToString()
2878+
Microsoft.FSharp.Core.ResultModule: System.Type GetType()
28722879
Microsoft.FSharp.Core.SealedAttribute: Boolean Equals(System.Object)
28732880
Microsoft.FSharp.Core.SealedAttribute: Boolean IsDefaultAttribute()
28742881
Microsoft.FSharp.Core.SealedAttribute: Boolean Match(System.Object)

src/fsharp/FSharp.Core.Unittests/SurfaceArea.portable78.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2856,6 +2856,13 @@ Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.Object get_
28562856
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.String ToString()
28572857
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: System.Type GetType()
28582858
Microsoft.FSharp.Core.RequiresExplicitTypeArgumentsAttribute: Void .ctor()
2859+
Microsoft.FSharp.Core.ResultModule: Boolean Equals(System.Object)
2860+
Microsoft.FSharp.Core.ResultModule: Int32 GetHashCode()
2861+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[T,TResult] MapError[TError,TResult,T](Microsoft.FSharp.Core.FSharpFunc`2[TError,TResult], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2862+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[TResult,TError] Bind[T,TResult,TError](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpResult`2[TResult,TError]], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2863+
Microsoft.FSharp.Core.ResultModule: Microsoft.FSharp.Core.FSharpResult`2[TResult,TError] Map[T,TResult,TError](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], Microsoft.FSharp.Core.FSharpResult`2[T,TError])
2864+
Microsoft.FSharp.Core.ResultModule: System.String ToString()
2865+
Microsoft.FSharp.Core.ResultModule: System.Type GetType()
28592866
Microsoft.FSharp.Core.SealedAttribute: Boolean Equals(System.Object)
28602867
Microsoft.FSharp.Core.SealedAttribute: Boolean IsDefaultAttribute()
28612868
Microsoft.FSharp.Core.SealedAttribute: Boolean Match(System.Object)

src/fsharp/FSharp.Core/FSharp.Core.fsproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@
7373
<Compile Include="option.fs">
7474
<Link>Collections/option.fs</Link>
7575
</Compile>
76+
<Compile Include="result.fsi">
77+
<Link>Collections/result.fsi</Link>
78+
</Compile>
79+
<Compile Include="result.fs">
80+
<Link>Collections/result.fs</Link>
81+
</Compile>
7682
<Compile Include="collections.fsi">
7783
<Link>Collections/collections.fsi</Link>
7884
</Compile>

0 commit comments

Comments
 (0)