text: (self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[63650],{17375:function(e,t,n){"use strict";function a(e,t){if(null==e)return{};var n,a,r=function(e,t){if(null==e)return{};var n,a,r={},l=Object.keys(e);for(a=0;a<l.length;a++)n=l[a],t.indexOf(n)>=0||(r[n]=e[n]);return r}(e,t);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);for(a=0;a<l.length;a++)n=l[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(r[n]=e[n])}return r}n.d(t,{Z:function(){return a}})},30811:function(e,t,n){"use strict";n.r(t),n.d(t,{default:function(){return u}});var a=n(17375),r=n(96156),l=(n(67294),n(3905));function i(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}function p(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?i(Object(n),!0).forEach((function(t){(0,r.Z)(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):i(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}var s,o=(s="Intro",function(e){return console.warn("Component "+s+" was not imported, exported, or provided by MDXProvider as global scope"),(0,l.kt)("div",p({},e))}),m={};function u(e){var t=e.components,n=(0,a.Z)(e,["components"]);return(0,l.kt)("wrapper",p(p(p({},m),n),{},{components:t,mdxType:"MDXLayout"}),(0,l.kt)("h1",p({},{id:"result"}),"Result"),(0,l.kt)(o,{mdxType:"Intro"},(0,l.kt)("p",null,"Result types are really useful to describe the result of a certain operation\nwithout relying on exceptions or ",(0,l.kt)("inlineCode",{parentName:"p"},"option")," types."),(0,l.kt)("p",null,"This module gives you useful utilities to create and combine ",(0,l.kt)("inlineCode",{parentName:"p"},"Result")," data.")),(0,l.kt)("h2",p({},{id:"t"}),"t"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"prelude",prelude:!0}),"type t<'a, 'b> =\n  | Ok('a)\n  | Error('b)\n")),(0,l.kt)("p",null,"The type ",(0,l.kt)("inlineCode",{parentName:"p"},"Result.t(result, err)")," describes a variant of two states:\n",(0,l.kt)("inlineCode",{parentName:"p"},"Ok(someResult)")," represents a successful operation, whereby\n`",(0,l.kt)("inlineCode",{parentName:"p"},"Error(someError)")," signals an erronous operation."),(0,l.kt)("p",null,"In this concrete example, we are defining our own ",(0,l.kt)("inlineCode",{parentName:"p"},"Result")," type to reflect an HTTP like\nquery operation:"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"example",example:!0}),"type responseError = NotAvailable | NotFound\ntype queryResult = t<string, responseError>\n\nlet failQueryUser = (username: string): queryResult => {\n  Error(NotAvailable)\n}\n")),(0,l.kt)("h2",p({},{id:"getexn"}),"getExn"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let getExn: t<'a, 'b> => 'a\n")),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"getExn(res)"),": when ",(0,l.kt)("inlineCode",{parentName:"p"},"res")," is ",(0,l.kt)("inlineCode",{parentName:"p"},"Ok(n)"),", returns ",(0,l.kt)("inlineCode",{parentName:"p"},"n")," when ",(0,l.kt)("inlineCode",{parentName:"p"},"res")," is ",(0,l.kt)("inlineCode",{parentName:"p"},"Error(m)"),", raise an exception"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"example",example:!0}),'Belt.Result.getExn(Belt.Result.Ok(42)) == 42\n\nBelt.Result.getExn(Belt.Result.Error("Invalid data")) /* raises exception */\n')),(0,l.kt)("h2",p({},{id:"mapwithdefaultu"}),"mapWithDefaultU"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let mapWithDefaultU: (t<'a, 'c>, 'b, (. 'a) => 'b) => 'b\n")),(0,l.kt)("h2",p({},{id:"mapwithdefault"}),"mapWithDefault"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let mapWithDefault: (t<'a, 'c>, 'b, 'a => 'b) => 'b\n")),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"mapWithDefault(res, default, f)"),": When res is ",(0,l.kt)("inlineCode",{parentName:"p"},"Ok(n)"),", returns ",(0,l.kt)("inlineCode",{parentName:"p"},"f(n)"),",\notherwise ",(0,l.kt)("inlineCode",{parentName:"p"},"default"),"."),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"example",example:!0}),'let ok = Belt.Result.Ok(42)\nBelt.Result.mapWithDefault(ok, 0, (x) => x / 2) == 21\n\nlet error = Belt.Result.Error("Invalid data")\nBelt.Result.mapWithDefault(error, 0, (x) => x / 2) == 0\n')),(0,l.kt)("h2",p({},{id:"mapu"}),"mapU"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let mapU: (t<'a, 'c>, (. 'a) => 'b) => t<'b, 'c>\n")),(0,l.kt)("h2",p({},{id:"map"}),"map"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let map: (t<'a, 'c>, 'a => 'b) => t<'b, 'c>\n")),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"map(res, f)"),": When res is ",(0,l.kt)("inlineCode",{parentName:"p"},"Ok(n)"),", returns ",(0,l.kt)("inlineCode",{parentName:"p"},"Ok(f(n))"),". Otherwise returns res\nunchanged. Function ",(0,l.kt)("inlineCode",{parentName:"p"},"f")," takes a value of the same type as ",(0,l.kt)("inlineCode",{parentName:"p"},"n")," and returns an\nordinary value."),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"example",example:!0}),'let f = (x) => sqrt(Belt.Int.toFloat(x))\n\nBelt.Result.map(Ok(64), f) == Ok(8.0)\n\nBelt.Result.map(Error("Invalid data"), f) == Error("Invalid data")\n')),(0,l.kt)("h2",p({},{id:"flatmapu"}),"flatMapU"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let flatMapU: (t<'a, 'c>, (. 'a) => t<'b, 'c>) => t<'b, 'c>\n")),(0,l.kt)("h2",p({},{id:"flatmap"}),"flatMap"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let flatMap: (t<'a, 'c>, 'a => t<'b, 'c>) => t<'b, 'c>\n")),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"flatMap(res, f)"),": When res is ",(0,l.kt)("inlineCode",{parentName:"p"},"Ok(n)"),", returns ",(0,l.kt)("inlineCode",{parentName:"p"},"f(n)"),". Otherwise, returns res\nunchanged. Function ",(0,l.kt)("inlineCode",{parentName:"p"},"f")," takes a value of the same type as ",(0,l.kt)("inlineCode",{parentName:"p"},"n")," and returns a\n",(0,l.kt)("inlineCode",{parentName:"p"},"Belt.Result"),"."),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"example",example:!0}),'let recip = (x) =>\n  if (x !== 0.0) {\n    Belt.Result.Ok(1.0 /. x)\n  } else {\n    Belt.Result.Error("Divide by zero")\n  }\n\nBelt.Result.flatMap(Ok(2.0), recip) == Ok(0.5)\n\nBelt.Result.flatMap(Ok(0.0), recip) == Error("Divide by zero")\n\nBelt.Result.flatMap(Error("Already bad"), recip) == Error("Already bad")\n')),(0,l.kt)("h2",p({},{id:"getwithdefault"}),"getWithDefault"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let getWithDefault: (t<'a, 'b>, 'a) => 'a\n")),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"getWithDefault(res, defaultValue)"),": If ",(0,l.kt)("inlineCode",{parentName:"p"},"res")," is ",(0,l.kt)("inlineCode",{parentName:"p"},"Ok(n)"),", returns ",(0,l.kt)("inlineCode",{parentName:"p"},"n"),",\notherwise ",(0,l.kt)("inlineCode",{parentName:"p"},"default")),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"example",example:!0}),'Belt.Result.getWithDefault(Ok(42), 0) == 42\n\nBelt.Result.getWithDefault(Error("Invalid Data"), 0) == 0\n')),(0,l.kt)("h2",p({},{id:"isok"}),"isOk"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let isOk: t<'a, 'b> => bool\n")),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"isOk(res)"),": Returns ",(0,l.kt)("inlineCode",{parentName:"p"},"true")," if ",(0,l.kt)("inlineCode",{parentName:"p"},"res")," is of the form ",(0,l.kt)("inlineCode",{parentName:"p"},"Ok(n)"),", ",(0,l.kt)("inlineCode",{parentName:"p"},"false")," if it is\nthe ",(0,l.kt)("inlineCode",{parentName:"p"},"Error(e)")," variant."),(0,l.kt)("h2",p({},{id:"iserror"}),"isError"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let isError: t<'a, 'b> => bool\n")),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"isError(res)"),": Returns ",(0,l.kt)("inlineCode",{parentName:"p"},"true")," if ",(0,l.kt)("inlineCode",{parentName:"p"},"res")," is of the form ",(0,l.kt)("inlineCode",{parentName:"p"},"Error(e)"),", ",(0,l.kt)("inlineCode",{parentName:"p"},"false")," if\nit is the ",(0,l.kt)("inlineCode",{parentName:"p"},"Ok(n)")," variant."),(0,l.kt)("h2",p({},{id:"equ"}),"eqU"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let eqU: (t<'a, 'c>, t<'b, 'd>, (. 'a, 'b) => bool) => bool\n")),(0,l.kt)("h2",p({},{id:"eq"}),"eq"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let eq: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => bool) => bool\n")),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"eq(res1, res2, f)"),": Determine if two ",(0,l.kt)("inlineCode",{parentName:"p"},"Belt.Result")," variables are equal with\nrespect to an equality function. If ",(0,l.kt)("inlineCode",{parentName:"p"},"res1")," and ",(0,l.kt)("inlineCode",{parentName:"p"},"res2")," are of the form ",(0,l.kt)("inlineCode",{parentName:"p"},"Ok(n)"),"\nand ",(0,l.kt)("inlineCode",{parentName:"p"},"Ok(m)"),", return the result of ",(0,l.kt)("inlineCode",{parentName:"p"},"f(n, m)"),". If one of ",(0,l.kt)("inlineCode",{parentName:"p"},"res1")," and ",(0,l.kt)("inlineCode",{parentName:"p"},"res2")," are of\nthe form ",(0,l.kt)("inlineCode",{parentName:"p"},"Error(e)"),", return false If both ",(0,l.kt)("inlineCode",{parentName:"p"},"res1")," and ",(0,l.kt)("inlineCode",{parentName:"p"},"res2")," are of the form\n",(0,l.kt)("inlineCode",{parentName:"p"},"Error(e)"),", return true"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"example",example:!0}),'let good1 = Belt.Result.Ok(42)\n\nlet good2 = Belt.Result.Ok(32)\n\nlet bad1 = Belt.Result.Error("invalid")\n\nlet bad2 = Belt.Result.Error("really invalid")\n\nlet mod10equal = (a, b) => mod(a, 10) === mod(b, 10)\n\nBelt.Result.eq(good1, good2, mod10equal) == true\n\nBelt.Result.eq(good1, bad1, mod10equal) == false\n\nBelt.Result.eq(bad2, good2, mod10equal) == false\n\nBelt.Result.eq(bad1, bad2, mod10equal) == true\n')),(0,l.kt)("h2",p({},{id:"cmpu"}),"cmpU"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let cmpU: (t<'a, 'c>, t<'b, 'd>, (. 'a, 'b) => int) => int\n")),(0,l.kt)("h2",p({},{id:"cmp"}),"cmp"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"sig",sig:!0}),"let cmp: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => int) => int\n")),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"cmp(res1, res2, f)"),": Compare two ",(0,l.kt)("inlineCode",{parentName:"p"},"Belt.Result"),' variables with respect to a\ncomparison function. The comparison function returns -1 if the first variable\nis "less than" the second, 0 if the two variables are equal, and 1 if the first\nis "greater than" the second.'),(0,l.kt)("p",null,"If ",(0,l.kt)("inlineCode",{parentName:"p"},"res1")," and ",(0,l.kt)("inlineCode",{parentName:"p"},"res2")," are of the form ",(0,l.kt)("inlineCode",{parentName:"p"},"Ok(n)")," and ",(0,l.kt)("inlineCode",{parentName:"p"},"Ok(m)"),", return the result of\n",(0,l.kt)("inlineCode",{parentName:"p"},"f(n, m)"),". If ",(0,l.kt)("inlineCode",{parentName:"p"},"res1")," is of the form ",(0,l.kt)("inlineCode",{parentName:"p"},"Error(e)")," and ",(0,l.kt)("inlineCode",{parentName:"p"},"res2")," of the form ",(0,l.kt)("inlineCode",{parentName:"p"},"Ok(n)"),",\nreturn -1 (nothing is less than something) If ",(0,l.kt)("inlineCode",{parentName:"p"},"res1")," is of the form ",(0,l.kt)("inlineCode",{parentName:"p"},"Ok(n)")," and\n",(0,l.kt)("inlineCode",{parentName:"p"},"res2")," of the form ",(0,l.kt)("inlineCode",{parentName:"p"},"Error(e)"),", return 1 (something is greater than nothing) If\nboth ",(0,l.kt)("inlineCode",{parentName:"p"},"res1")," and ",(0,l.kt)("inlineCode",{parentName:"p"},"res2")," are of the form ",(0,l.kt)("inlineCode",{parentName:"p"},"Error(e)"),", return 0 (equal)"),(0,l.kt)("pre",null,(0,l.kt)("code",p({parentName:"pre"},{className:"language-res",metastring:"example",example:!0}),'let good1 = Belt.Result.Ok(59)\n\nlet good2 = Belt.Result.Ok(37)\n\nlet bad1 = Belt.Result.Error("invalid")\n\nlet bad2 = Belt.Result.Error("really invalid")\n\nlet mod10cmp = (a, b) => Pervasives.compare(mod(a, 10), mod(b, 10))\n\nBelt.Result.cmp(Ok(39), Ok(57), mod10cmp) == 1\n\nBelt.Result.cmp(Ok(57), Ok(39), mod10cmp) == (-1)\n\nBelt.Result.cmp(Ok(39), Error("y"), mod10cmp) == 1\n\nBelt.Result.cmp(Error("x"), Ok(57), mod10cmp) == (-1)\n\nBelt.Result.cmp(Error("x"), Error("y"), mod10cmp) == 0\n')))}u.isMDXComponent=!0,u.frontmatter={__ghEditHref:"https://github.com/reason-association/rescript-lang.org/blob/master/pages/docs/manual/latest/api/belt/result.mdx"}},2731:function(e,t,n){(window.__NEXT_P=window.__NEXT_P||[]).push(["/docs/manual/latest/api/belt/result",function(){return n(30811)}])}},function(e){e.O(0,[92888,49774,40179],(function(){return t=2731,e(e.s=t);var t}));var t=e.O();_N_E=t}]);