namespace FsToolkit.ErrorHandling #if !FABLE_COMPILER [] module ValueOption = let inline bind ([] f) x = match x with | ValueSome v -> f v | ValueNone -> ValueNone let inline map ([] f) x = match x with | ValueSome v -> ValueSome(f v) | ValueNone -> ValueNone let inline ofOption (opt: 'value option) : 'value voption = match opt with | Some v -> ValueSome v | None -> ValueNone let inline toOption (vopt: 'value voption) : 'value option = match vopt with | ValueSome v -> Some v | ValueNone -> None let inline traverseResult ([] binder: 'okInput -> Result<'okOutput, 'error>) (input: 'okInput voption) : Result<'okOutput voption, 'error> = match input with | ValueNone -> Ok ValueNone | ValueSome v -> binder v |> Result.map ValueSome let inline sequenceResult (opt: Result<'okOutput, 'error> voption) : Result<'okOutput voption, 'error> = traverseResult id opt let inline tryParse< ^value when ^value: (static member TryParse: string * byref< ^value > -> bool)> (valueToParse: string) : ^value voption = let mutable output = Unchecked.defaultof< ^value> let parsed = (^value: (static member TryParse: string * byref< ^value > -> bool) (valueToParse, &output)) match parsed with | true -> ValueSome output | _ -> ValueNone let inline tryGetValue (key: 'key) (dictionary: ^Dictionary) : ^value voption = let mutable output = Unchecked.defaultof< ^value> let parsed = (^Dictionary: (member TryGetValue: 'key * byref< ^value > -> bool) (dictionary, key, &output)) match parsed with | true -> ValueSome output | false -> ValueNone /// /// Takes two voptions and returns a tuple of the pair or none if either are none /// /// The input option /// The input option /// let inline zip (left: 'left voption) (right: 'right voption) : ('left * 'right) voption = match left, right with | ValueSome v1, ValueSome v2 -> ValueSome(v1, v2) | _ -> ValueNone let inline ofResult (result: Result<'ok, 'error>) : 'ok voption = match result with | Ok v -> ValueSome v | Error _ -> ValueNone /// /// Convert a potentially null value to an ValueOption. /// /// This is different from ValueOption.ofObj where it doesn't require the value to be constrained to null. /// This is beneficial where third party APIs may generate a record type using reflection and it can be null. /// See Null-checking considerations in F# for more details. /// /// The potentially null value /// An ValueOption /// let inline ofNull (value: 'nullableValue) = if System.Object.ReferenceEquals(value, null) then ValueNone else ValueSome value /// /// /// bindNull binder voption evaluates to match voption with ValueNone -> ValueNone | ValueSome x -> binder x |> ValueOption.ofNull /// /// Automatically onverts the result of binder that is pontentially null into an Valueoption. /// /// A function that takes the value of type 'value from an voption and transforms it into /// a value of type 'nullableValue. /// The input voption /// /// /// A voption of the output type of the binder. /// let inline bindNull ([] binder: 'value -> 'nullableValue) (voption: ValueOption<'value>) : 'nullableValue voption = match voption with | ValueSome x -> binder x |> ofNull | ValueNone -> ValueNone /// /// Returns result of running if it is ValueSome, otherwise returns result of running /// /// The function to run if is ValueSome /// The function to run if is ValueNone /// The input option. /// /// The result of running if the input is ValueSome, else returns result of running . /// let inline either ([] onSome: 'a -> 'output) ([] onNone: unit -> 'output) (input: 'a voption) : 'output = match input with | ValueSome x -> onSome x | ValueNone -> onNone () let inline ofPair (input: bool * 'a) = match input with | true, x -> ValueSome x | false, _ -> ValueNone #endif