Header menu logo FSharp.Core

Result Module

Contains operations for working with values of type Result.

Functions and values

Function or value Description

bind binder result

Full Usage: bind binder result

Parameters:
    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.
Modifiers: inline
Type parameters: 'T, 'U, 'TError

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.

Example

 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 Clamp: value: int * min: int * max: int -> int static member CopySign: value: int * sign: int -> int static member CreateChecked<'TOther (requires 'TOther :> INumberBase<'TOther>)> : value: 'TOther -> 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>

contains value result

Full Usage: contains value result

Parameters:
    value : 'T - The value to test for equality.
    result : Result<'T, 'Error> - The input result.

Returns: bool True if the result is Ok and contains a value equal to value, otherwise false.
Modifiers: inline
Type parameters: 'T, 'Error

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 Ok and contains a value equal to value, otherwise false.

Example

 (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 result

Full Usage: count result

Parameters:
    result : Result<'T, 'Error> - The input result.

Returns: int A zero if the result is Error, a one otherwise.
Modifiers: inline
Type parameters: 'T, 'Error

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.

Example

 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>

defaultValue value result

Full Usage: defaultValue value result

Parameters:
    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.
Modifiers: inline
Type parameters: 'T, 'Error

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.

Example

 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>

defaultWith defThunk result

Full Usage: defaultWith defThunk result

Parameters:
    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.
Modifiers: inline
Type parameters: 'Error, 'T

Gets the value of the result if the result is Ok, otherwise evaluates defThunk and returns the result.

defThunk is not evaluated unless result is Error.

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.

Example

 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 predicate result

Full Usage: exists predicate result

Parameters:
    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.
Modifiers: inline
Type parameters: 'T, 'Error

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.

Example

 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 folder state result

Full Usage: fold folder state result

Parameters:
    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.
Modifiers: inline
Type parameters: 'T, 'Error, 'State

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.

Example

 (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 folder result state

Full Usage: foldBack folder result state

Parameters:
    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.
Modifiers: inline
Type parameters: 'T, 'Error, 'State

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.

Example

 (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 predicate result

Full Usage: forall predicate result

Parameters:
    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.
Modifiers: inline
Type parameters: 'T, 'Error

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.

Example

 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>

isError result

Full Usage: isError result

Parameters:
    result : Result<'T, 'Error> - The input result.

Returns: bool True if the result is Error.
Modifiers: inline
Type parameters: 'T, 'Error

Returns true if the result is Error.

result : Result<'T, 'Error>

The input result.

Returns: bool

True if the result is Error.

Example

 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>

isOk result

Full Usage: isOk result

Parameters:
    result : Result<'T, 'Error> - The input result.

Returns: bool True if the result is OK.
Modifiers: inline
Type parameters: 'T, 'Error

Returns true if the result is Ok.

result : Result<'T, 'Error>

The input result.

Returns: bool

True if the result is OK.

Example

 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 action result

Full Usage: iter action result

Parameters:
    action : 'T -> unit - A function to apply to the result value.
    result : Result<'T, 'Error> - The input result.

Modifiers: inline
Type parameters: 'T, 'Error

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.

Example

 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 mapping result

Full Usage: map mapping result

Parameters:
    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.
Modifiers: inline
Type parameters: 'T, 'U, 'TError

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.

Example

 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

mapError mapping result

Full Usage: mapError mapping result

Parameters:
    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.
Modifiers: inline
Type parameters: 'TError, 'U, 'T

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.

Example

 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

toArray result

Full Usage: toArray result

Parameters:
    result : Result<'T, 'Error> - The input result.

Returns: 'T array The result array.
Modifiers: inline
Type parameters: 'T, 'Error

Convert the result to an array of length 0 or 1.

result : Result<'T, 'Error>

The input result.

Returns: 'T array

The result array.

Example

 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>

toList result

Full Usage: toList result

Parameters:
    result : Result<'T, 'Error> - The input result.

Returns: 'T list The result list.
Modifiers: inline
Type parameters: 'T, 'Error

Convert the result to a list of length 0 or 1.

result : Result<'T, 'Error>

The input result.

Returns: 'T list

The result list.

Example

 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>

toOption result

Full Usage: toOption result

Parameters:
    result : Result<'T, 'Error> - The input result.

Returns: 'T option The option value.
Modifiers: inline
Type parameters: 'T, 'Error

Convert the result to an Option value.

result : Result<'T, 'Error>

The input result.

Returns: 'T option

The option value.

Example

 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>

toValueOption result

Full Usage: toValueOption result

Parameters:
    result : Result<'T, 'Error> - The input result.

Returns: 'T voption The result value.
Modifiers: inline
Type parameters: 'T, 'Error

Convert the result to an Option value.

result : Result<'T, 'Error>

The input result.

Returns: 'T voption

The result value.

Example

 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>

Type something to start searching.