This is the latest docs version
Quick Links
  • -Overview
  • -Language Features
  • -JS Interop
  • -Build System
Documentation
Language Manual
Reference for all language features
ReScript & React
First class bindings for ReactJS
GenType
Seamless TypeScript integration
Reanalyze
Dead Code & Termination analysis
Exploration
Packages
Explore third party libraries and bindings
Syntax Lookup
Discover all syntax constructs
APIPlaygroundBlogCommunity
  • Playground
  • Blog
  • Twitter
  • GitHub
  • Forum
Core Module
Overview
Core
submodules
  • Array
  • ArrayBuffer
  • AsyncIterator
  • BigInt
  • BigInt64Array
    • Constants
    BigUint64Array
    • Constants
  • Console
  • DataView
  • Date
    • UTC
  • Dict
  • Error
    • URIError
    • TypeError
    • SyntaxError
    • ReferenceError
    • RangeError
    • EvalError
  • Exn
  • Float
    • Constants
    Float32Array
    • Constants
    Float64Array
    • Constants
    Int
    • Constants
    Int16Array
    • Constants
    Int32Array
    • Constants
    Int8Array
    • Constants
  • Internal
  • Intl
    • Segments
    • Segmenter
    • RelativeTimeFormat
    • PluralRules
    • NumberFormat
      • Grouping
    • Locale
    • ListFormat
    • DateTimeFormat
    • Collator
    • Common
  • Iterator
  • JSON
    • Decode
    • Encode
    • Classify
  • List
  • Map
  • MapperRt
  • Math
    • Int
    • Constants
  • Null
  • Nullable
  • Object
  • Option
  • Ordering
  • Promise
  • Re
    • Result
    RegExp
    • Result
  • Result
    • v
      getExn
    • v
      mapOr
    • v
      mapWithDefault
      D
    • v
      map
    • v
      flatMap
    • v
      getOr
    • v
      getWithDefault
      D
    • v
      isOk
    • v
      isError
    • v
      equal
    • v
      compare
    • v
      forEach
    • v
      mapError
  • Set
  • String
  • Symbol
  • Type
    • Classify
  • TypedArray
  • Uint16Array
    • Constants
    Uint32Array
    • Constants
    Uint8Array
    • Constants
    Uint8ClampedArray
    • Constants
  • WeakMap
  • WeakSet
  • API / Core / Result

    Result

    getExn

    RESCRIPT
    let getExn: result<'a, 'b> => 'a

    Result types are really useful to describe the result of a certain operation without relying on exceptions or option types.

    This module gives you useful utilities to create and combine Result data.

    mapOr

    RESCRIPT
    let mapOr: (result<'a, 'c>, 'b, 'a => 'b) => 'b

    mapOr(res, default, f): When res is Ok(n), returns f(n), otherwise default.

    Examples

    RESCRIPT
    let ok = Ok(42) Result.mapOr(ok, 0, (x) => x / 2) == 21 let error = Error("Invalid data") Result.mapOr(error, 0, (x) => x / 2) == 0

    mapWithDefault

    Deprecated

    Use mapOr instead

    RESCRIPT
    let mapWithDefault: (result<'a, 'c>, 'b, 'a => 'b) => 'b

    map

    RESCRIPT
    let map: (result<'a, 'c>, 'a => 'b) => result<'b, 'c>

    map(res, f): When res is Ok(n), returns Ok(f(n)). Otherwise returns res unchanged. Function f takes a value of the same type as n and returns an ordinary value.

    Examples

    RESCRIPT
    let f = (x) => sqrt(Int.toFloat(x)) Result.map(Ok(64), f) == Ok(8.0) Result.map(Error("Invalid data"), f) == Error("Invalid data")

    flatMap

    RESCRIPT
    let flatMap: (result<'a, 'c>, 'a => result<'b, 'c>) => result<'b, 'c>

    flatMap(res, f): When res is Ok(n), returns f(n). Otherwise, returns res unchanged. Function f takes a value of the same type as n and returns a Result.

    Examples

    RESCRIPT
    let recip = (x) => if (x !== 0.0) { Ok(1.0 /. x) } else { Error("Divide by zero") } Result.flatMap(Ok(2.0), recip) == Ok(0.5) Result.flatMap(Ok(0.0), recip) == Error("Divide by zero") Result.flatMap(Error("Already bad"), recip) == Error("Already bad")

    getOr

    RESCRIPT
    let getOr: (result<'a, 'b>, 'a) => 'a

    getOr(res, defaultValue): If res is Ok(n), returns n, otherwise default

    Examples

    RESCRIPT
    Result.getOr(Ok(42), 0) == 42 Result.getOr(Error("Invalid Data"), 0) == 0

    getWithDefault

    Deprecated

    Use getOr instead

    RESCRIPT
    let getWithDefault: (result<'a, 'b>, 'a) => 'a

    isOk

    RESCRIPT
    let isOk: result<'a, 'b> => bool

    isOk(res): Returns true if res is of the form Ok(n), false if it is the Error(e) variant.

    isError

    RESCRIPT
    let isError: result<'a, 'b> => bool

    isError(res): Returns true if res is of the form Error(e), false if it is the Ok(n) variant.

    equal

    RESCRIPT
    let equal: (result<'a, 'c>, result<'b, 'd>, ('a, 'b) => bool) => bool

    equal(res1, res2, f): Determine if two Result variables are equal with respect to an equality function. If res1 and res2 are of the form Ok(n) and Ok(m), return the result of f(n, m). If one of res1 and res2 are of the form Error(e), return false If both res1 and res2 are of the form Error(e), return true

    Examples

    RESCRIPT
    let good1 = Ok(42) let good2 = Ok(32) let bad1 = Error("invalid") let bad2 = Error("really invalid") let mod10equal = (a, b) => mod(a, 10) === mod(b, 10) Result.equal(good1, good2, mod10equal) == true Result.equal(good1, bad1, mod10equal) == false Result.equal(bad2, good2, mod10equal) == false Result.equal(bad1, bad2, mod10equal) == true

    compare

    RESCRIPT
    let compare: ( result<'a, 'c>, result<'b, 'd>, ('a, 'b) => Core__Ordering.t, ) => Core__Ordering.t

    compare(res1, res2, f): Compare two Result variables with respect to a comparison function. The comparison function returns -1. if the first variable is "less than" the second, 0. if the two variables are equal, and 1. if the first is "greater than" the second.

    If res1 and res2 are of the form Ok(n) and Ok(m), return the result of f(n, m). If res1 is of the form Error(e) and res2 of the form Ok(n), return -1. (nothing is less than something) If res1 is of the form Ok(n) and res2 of the form Error(e), return 1. (something is greater than nothing) If both res1 and res2 are of the form Error(e), return 0. (equal)

    Examples

    RESCRIPT
    let good1 = Ok(59) let good2 = Ok(37) let bad1 = Error("invalid") let bad2 = Error("really invalid") let mod10cmp = (a, b) => Int.compare(mod(a, 10), mod(b, 10)) Result.compare(Ok(39), Ok(57), mod10cmp) == 1. Result.compare(Ok(57), Ok(39), mod10cmp) == (-1.) Result.compare(Ok(39), Error("y"), mod10cmp) == 1. Result.compare(Error("x"), Ok(57), mod10cmp) == (-1.) Result.compare(Error("x"), Error("y"), mod10cmp) == 0.

    forEach

    RESCRIPT
    let forEach: (result<'a, 'b>, 'a => unit) => unit

    forEach(res, f) runs the provided function f on the Ok value. If res is Error, nothing happens.

    Examples

    RESCRIPT
    Result.forEach(Ok(3), Console.log) // Logs "3", returns () Result.forEach(Error("x"), Console.log) // Does nothing, returns ()

    mapError

    RESCRIPT
    let mapError: (result<'a, 'b>, 'b => 'c) => result<'a, 'c>

    mapError(r, f) generates a new result by applying the function f to the Error value. If the source is Ok, return it as-is.

    Examples

    RESCRIPT
    let format = n => `Error code: ${n->Int.toString}` Result.mapError(Error(14), format) // Error("Error code: 14") Result.mapError(Ok("abc"), format) // Ok("abc")
    Types and values
    • v
      getExn
    • v
      mapOr
    • v
      mapWithDefault
      D
    • v
      map
    • v
      flatMap
    • v
      getOr
    • v
      getWithDefault
      D
    • v
      isOk
    • v
      isError
    • v
      equal
    • v
      compare
    • v
      forEach
    • v
      mapError

    © 2024 The ReScript Project

    Software and assets distribution powered by KeyCDN.

    About
    • Community
    • ReScript Association
    Find us on