Result (FSharp.Core)
bind f inp evaluates to match inp with Error e -> Error e | Ok x -> f x
bind f inp evaluates to match inp with Error e -> Error e | Ok x -> f x
-
binder
:
'T -> Result<'U, 'TError> -
A function that takes the value of type T from a result and transforms it into a result containing a value of type U.
-
result
:
Result<'T, 'TError> -
The input result.
-
Returns:
Result<'U, 'TError> -
A result of the output type of the binder.
let tryParse (input: string) =
match System.Int32.TryParse input with
| true, v -> Ok v
| false, _ -> Error "couldn't parse"
Error "message" |> Result.bind tryParse // evaluates to Error "message"
Ok "42" |> Result.bind tryParse // evaluates to Ok 42
Ok "Forty-two" |> Result.bind tryParse // evaluates to Error "couldn't parse"
val tryParse: input: string -> Result<int,string>
val input: string
Multiple items
val string: value: 'T -> string
--------------------
type string = System.String
namespace System
[<Struct>]
type Int32 =
member CompareTo: value: int -> int + 1 overload
member Equals: obj: int -> bool + 1 overload
member GetHashCode: unit -> int
member GetTypeCode: unit -> TypeCode
member ToString: unit -> string + 3 overloads
member TryFormat: utf8Destination: Span<byte> * bytesWritten: byref<int> * ?format: ReadOnlySpan<char> * ?provider: IFormatProvider -> bool + 1 overload
static member Abs: value: int -> int
static member BigMul: left: int * right: int -> int64
static member Clamp: value: int * min: int * max: int -> int
static member CopySign: value: int * sign: int -> int
...
<summary>Represents a 32-bit signed integer.</summary>
System.Int32.TryParse(s: string, result: byref<int>) : bool
System.Int32.TryParse(s: System.ReadOnlySpan<char>, result: byref<int>) : bool
System.Int32.TryParse(utf8Text: System.ReadOnlySpan<byte>, result: byref<int>) : bool
System.Int32.TryParse(s: string, provider: System.IFormatProvider, result: byref<int>) : bool
System.Int32.TryParse(s: System.ReadOnlySpan<char>, provider: System.IFormatProvider, result: byref<int>) : bool
System.Int32.TryParse(utf8Text: System.ReadOnlySpan<byte>, provider: System.IFormatProvider, result: byref<int>) : bool
System.Int32.TryParse(s: string, style: System.Globalization.NumberStyles, provider: System.IFormatProvider, result: byref<int>) : bool
System.Int32.TryParse(s: System.ReadOnlySpan<char>, style: System.Globalization.NumberStyles, provider: System.IFormatProvider, result: byref<int>) : bool
System.Int32.TryParse(utf8Text: System.ReadOnlySpan<byte>, style: System.Globalization.NumberStyles, provider: System.IFormatProvider, result: byref<int>) : bool
val v: int
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val bind: binder: ('T -> Result<'U,'TError>) -> result: Result<'T,'TError> -> Result<'U,'TError>
Evaluates to true if result is Ok and its value is equal to value.
Evaluates to true if result is Ok and its value is equal to value.
-
value
:
'T -
The value to test for equality.
-
result
:
Result<'T, 'Error> -
The input result.
-
Returns:
bool -
True if the result is
Okand contains a value equal to value, otherwise false.
(99, Error 99) ||> Result.contains // evaluates to false
(99, Ok 99) ||> Result.contains // evaluates to true
(99, Ok 100) ||> Result.contains // evaluates to false
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val contains: value: 'T -> result: Result<'T,'Error> -> bool (requires equality)
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
count inp evaluates to match inp with Error _ -> 0 | Ok _ -> 1.
count inp evaluates to match inp with Error _ -> 0 | Ok _ -> 1.
-
result
:
Result<'T, 'Error> -
The input result.
-
Returns:
int -
A zero if the result is Error, a one otherwise.
Error 99 |> Result.count // evaluates to 0
Ok 99 |> Result.count // evaluates to 1
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val count: result: Result<'T,'Error> -> int
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
Gets the value of the result if the result is Ok, otherwise returns the specified default value.
Gets the value of the result if the result is Ok, otherwise returns the specified default value.
-
value
:
'T -
The specified default value.
-
result
:
Result<'T, 'Error> -
The input result.
-
Returns:
'T -
The result if the result is Ok, else the default value.
Result.defaultValue 2 (Error 3) // evaluates to 2
Result.defaultValue 2 (Ok 1) // evaluates to 1
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val defaultValue: value: 'T -> result: Result<'T,'Error> -> 'T
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
Gets the value of the result if the result is Ok, otherwise evaluates defThunk and returns the result.
Gets the value of the result if the result is Ok, otherwise evaluates defThunk and returns the result.
-
defThunk
:
'Error -> 'T -
A thunk that provides a default value when evaluated.
-
result
:
Result<'T, 'Error> -
The input result.
-
Returns:
'T -
The result if the result is Ok, else the result of evaluating defThunk.
Ok 1 |> Result.defaultWith (fun error -> 99) // evaluates to 1
Error 2 |> Result.defaultWith (fun error -> 99) // evaluates to 99
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val defaultWith: defThunk: ('Error -> 'T) -> result: Result<'T,'Error> -> 'T
val error: obj
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
val error: int
exists p inp evaluates to match inp with Error _ -> false | Ok x -> p x.
exists p inp evaluates to match inp with Error _ -> false | Ok x -> p x.
-
predicate
:
'T -> bool -
A function that evaluates to a boolean when given a value from the result type.
-
result
:
Result<'T, 'Error> -
The input result.
-
Returns:
bool -
False if the result is Error, otherwise it returns the result of applying the predicate to the result value.
Error 6 |> Result.exists (fun x -> x >= 5) // evaluates to false
Ok 42 |> Result.exists (fun x -> x >= 5) // evaluates to true
Ok 4 |> Result.exists (fun x -> x >= 5) // evaluates to false
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val exists: predicate: ('T -> bool) -> result: Result<'T,'Error> -> bool
val x: int
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
fold f s inp evaluates to match inp with Error _ -> s | Ok x -> f s x.
fold f s inp evaluates to match inp with Error _ -> s | Ok x -> f s x.
-
folder
:
'State -> 'T -> 'State -
A function to update the state data when given a value from an result.
-
state
:
'State -
The initial state.
-
result
:
Result<'T, 'Error> -
The input result.
-
Returns:
'State -
The original state if the result is Error, otherwise it returns the updated state with the folder and the result value.
(0, Error 2) ||> Result.fold (fun accum x -> accum + x * 2) // evaluates to 0
(0, Ok 1) ||> Result.fold (fun accum x -> accum + x * 2) // evaluates to 2
(10, Ok 1) ||> Result.fold (fun accum x -> accum + x * 2) // evaluates to 12
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val fold<'T,'Error,'State> : folder: ('State -> 'T -> 'State) -> state: 'State -> result: Result<'T,'Error> -> 'State
val accum: int
val x: int
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
foldBack f inp s evaluates to match inp with Error _ -> s | Ok x -> f x s.
foldBack f inp s evaluates to match inp with Error _ -> s | Ok x -> f x s.
-
folder
:
'T -> 'State -> 'State -
A function to update the state data when given a value from an result.
-
result
:
Result<'T, 'Error> -
The input result.
-
state
:
'State -
The initial state.
-
Returns:
'State -
The original state if the result is Error, otherwise it returns the updated state with the folder and the result value.
(Error 2, 0) ||> Result.foldBack (fun x accum -> accum + x * 2) // evaluates to 0
(Ok 1, 0) ||> Result.foldBack (fun x accum -> accum + x * 2) // evaluates to 2
(Ok 1, 10) ||> Result.foldBack (fun x accum -> accum + x * 2) // evaluates to 12
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val foldBack<'T,'Error,'State> : folder: ('T -> 'State -> 'State) -> result: Result<'T,'Error> -> state: 'State -> 'State
val x: int
val accum: int
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
forall p inp evaluates to match inp with Error _ -> true | Ok x -> p x.
forall p inp evaluates to match inp with Error _ -> true | Ok x -> p x.
-
predicate
:
'T -> bool -
A function that evaluates to a boolean when given a value from the result type.
-
result
:
Result<'T, 'Error> -
The input result.
-
Returns:
bool -
True if the result is Error, otherwise it returns the result of applying the predicate to the result value.
Error 1 |> Result.forall (fun x -> x >= 5) // evaluates to true
Ok 42 |> Result.forall (fun x -> x >= 5) // evaluates to true
Ok 4 |> Result.forall (fun x -> x >= 5) // evaluates to false
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val forall: predicate: ('T -> bool) -> result: Result<'T,'Error> -> bool
val x: int
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
Returns true if the result is Error.
Returns true if the result is Error.
-
result
:
Result<'T, 'Error> -
The input result.
-
Returns:
bool -
True if the result is Error.
Ok 42 |> Result.isError // evaluates to false
Error 42 |> Result.isError // evaluates to true
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val isError: result: Result<'T,'Error> -> bool
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
Returns true if the result is Ok.
Returns true if the result is Ok.
-
result
:
Result<'T, 'Error> -
The input result.
-
Returns:
bool -
True if the result is OK.
Ok 42 |> Result.isOk // evaluates to true
Error 42 |> Result.isOk // evaluates to false
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val isOk: result: Result<'T,'Error> -> bool
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
iter f inp executes match inp with Error _ -> () | Ok x -> f x.
iter f inp executes match inp with Error _ -> () | Ok x -> f x.
-
action
:
'T -> unit -
A function to apply to the result value.
-
result
:
Result<'T, 'Error> -
The input result.
Error "Hello world" |> Result.iter (printfn "%s") // does nothing
Ok "Hello world" |> Result.iter (printfn "%s") // prints "Hello world"
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val iter: action: ('T -> unit) -> result: Result<'T,'Error> -> unit
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
map f inp evaluates to match inp with Error e -> Error e | Ok x -> Ok (f x).
map f inp evaluates to match inp with Error e -> Error e | Ok x -> Ok (f x).
-
mapping
:
'T -> 'U -
A function to apply to the OK result value.
-
result
:
Result<'T, 'TError> -
The input result.
-
Returns:
Result<'U, 'TError> -
A result of the input value after applying the mapping function, or Error if the input is Error.
Ok 1 |> Result.map (fun x -> "perfect") // evaluates to Ok "perfect"
Error "message" |> Result.map (fun x -> "perfect") // evaluates to Error "message"
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val map: mapping: ('T -> 'U) -> result: Result<'T,'TError> -> Result<'U,'TError>
val x: int
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
val x: obj
map f inp evaluates to match inp with Error x -> Error (f x) | Ok v -> Ok v.
map f inp evaluates to match inp with Error x -> Error (f x) | Ok v -> Ok v.
-
mapping
:
'TError -> 'U -
A function to apply to the Error result value.
-
result
:
Result<'T, 'TError> -
The input result.
-
Returns:
Result<'T, 'U> -
A result of the error value after applying the mapping function, or Ok if the input is Ok.
Ok 1 |> Result.mapError (fun x -> "bar") // evaluates to Ok 1
Error "foo" |> Result.mapError (fun x -> "bar") // evaluates to Error "bar"
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val mapError: mapping: ('TError -> 'U) -> result: Result<'T,'TError> -> Result<'T,'U>
val x: obj
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
val x: string
Convert the result to an array of length 0 or 1.
Convert the result to an array of length 0 or 1.
-
result
:
Result<'T, 'Error> -
The input result.
-
Returns:
'T array -
The result array.
Error 42 |> Result.toArray // evaluates to [||]
Ok 42 |> Result.toArray // evaluates to [| 42 |]
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val toArray: result: Result<'T,'Error> -> 'T array
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
Convert the result to a list of length 0 or 1.
Convert the result to a list of length 0 or 1.
-
result
:
Result<'T, 'Error> -
The input result.
-
Returns:
'T list -
The result list.
Error 42 |> Result.toList // evaluates to []
Ok 42 |> Result.toList // evaluates to [ 42 ]
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val toList: result: Result<'T,'Error> -> 'T list
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
Convert the result to an Option value.
Convert the result to an Option value.
-
result
:
Result<'T, 'Error> -
The input result.
-
Returns:
'T option -
The option value.
Error 42 |> Result.toOption // evaluates to None
Ok 42 |> Result.toOption // evaluates to Some 42
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val toOption: result: Result<'T,'Error> -> 'T option
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
Convert the result to an Option value.
Convert the result to an Option value.
-
result
:
Result<'T, 'Error> -
The input result.
-
Returns:
'T voption -
The result value.
Error 42 |> Result.toOption // evaluates to ValueNone
Ok 42 |> Result.toOption // evaluates to ValueSome 42
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
Multiple items
module Result
from Microsoft.FSharp.Core
--------------------
[<Struct>]
type Result<'T,'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError
val toOption: result: Result<'T,'Error> -> 'T option
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>