Skip to content

Commit a76958b

Browse files
authored
Deprecate some functions in Validation (#248)
1 parent c8c2842 commit a76958b

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/FSharpPlus/Validations.fs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ module Validation=
7777
| Success a -> f a
7878

7979
/// orElse v a returns 'a when v is Failure, and the a in Success a.
80-
let orElse v (a: 'a) =
81-
match v with
82-
| Failure _ -> a
83-
| Success x -> x
80+
[<System.Obsolete("Use Validation.defaultValue instead.")>]
81+
let orElse v (a: 'a) = match v with | Failure _ -> a | Success x -> x
82+
/// defaultValue value source returns value when source is Failure, and the v in Success v.
83+
let defaultValue (value:'a) (source:Validation<'err,'a>) :'a = match source with Success v -> v | _ -> value
84+
/// defaultWith returns either x when the source is Success x, otherwise applies the function f on e if the source is Failure e.
85+
let defaultWith (f:'err->'a) (source:Validation<'err,'a>) :'a = match source with | Success x -> x | Failure e -> f e
8486

8587
/// Return the 'a or run the given function over the 'e.
86-
let valueOr ea (v: Validation<'e,'a>) =
87-
match v with
88-
| Failure e -> ea e
89-
| Success a -> a
88+
[<System.Obsolete("Use Validation.defaultWith instead.")>]
89+
let valueOr ea (v: Validation<'e,'a>) = match v with | Failure e -> ea e | Success a -> a
9090

9191
/// 'liftResult' is useful for converting a 'Result' to an 'Validation'
9292
/// when the 'Error' of the 'Result' needs to be lifted into a 'Semigroup'.
@@ -113,6 +113,7 @@ module Validation=
113113
///
114114
/// validate : 'e -> ('a -> bool) -> 'a -> Validation<'e, 'a>
115115
///
116+
[<System.Obsolete("This function will not be supported in future versions.")>]
116117
let validate (e: 'e) (p: 'a -> bool) (a: 'a) : Validation<'e,'a> = if p a then Success a else Failure e
117118

118119
#if !FABLE_COMPILER
@@ -128,6 +129,7 @@ module Validation=
128129
/// This can be thought of as having the less general type:
129130
///
130131
/// ensure : 'e -> ('a -> 'bool) -> Validation<'a,'e> -> Validation<'a,'e>
132+
[<System.Obsolete("This function will not be supported in future versions.")>]
131133
let ensure (e: 'e) (p: 'a-> bool) = function
132134
| Failure x -> Failure x
133135
| Success a -> validate e p a

tests/FSharpPlus.Tests/Validations.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,15 @@ let testEnsureRightTrue () =
169169
areEqual (Success seven) subject
170170

171171
[<Test>]
172-
let testOrElseRight () =
173-
let v = Success seven
174-
let subject = Validation.orElse v three
172+
let testDefaultValueSuccess () =
173+
let v = Success seven
174+
let subject = Validation.defaultValue three v
175175
areEqual seven subject
176176

177177
[<Test>]
178-
let testOrElseLeft () =
178+
let testDefaultValueFailure () =
179179
let v = Failure seven
180-
let subject = Validation.orElse v three
180+
let subject = Validation.defaultValue three v
181181
areEqual three subject
182182

183183
//testEnsureLeftFalse, testEnsureLeftTrue, testEnsureRightFalse, testEnsureRightTrue,

0 commit comments

Comments
 (0)