Skip to main content

Result

Error handling with the Result type.

Result<T, E> is the type used for returning a possible error. It is a class with the constructor variants, Result:Ok(T), representing success and containing a value, and Result:Err(E), representing error and containing an error value.

local result: Result<number, string>

-- Both variants `Ok` and `Err` fit into the type
result = Ok(21)
result = Err("some error")

Functions return Result whenever errors are expected and recoverable.

A simple function returning Result might be defined and used like so:

local PAT = "v(%d+)"

function parseVersion(str: string): Result<number, string>
	local version: number = string.match(str, PAT)

	if version == 0 then
		return Err("Version cannot be zero")
	else if version == nil then
		return Err("Invalid string")
	end

	return Ok(version)
end

print("Successful result: " .. parseVersion("v12"):display())
print("Error result: " .. parseVersion("vString"):display())

Result comes with some convenience methods that make working with it more succinct.

local goodResult: Result<number, number> = Ok(10)
local badResult: Result<number, number> = Err(10)

-- The `isOk`and `isErr` methods do what they say.
assert(goodResult:isOk() and not goodResult:isErr())
assert(badResult:isErr() and not badResult:isOk())

-- `map` produces a new result from an existing one 
goodResult = goodResult:map(function(i) return i + 1 end)
badResult = badResult:map(function(i) return i - 1 end)

-- Use `and_then` to continue the computation.
local anotherGoodResult: Result<boolean, number> = goodResult:andThen(function(i) return Ok(i == 11) end)

-- Use `or_else` to handle the error
local badResult: Result<number, number> = bad_result:orElse(function(i) return Ok(i + 20) end)

-- Get the internal `Ok` value and panic if there is an `Err`
local finalAwesomeResult: boolean = anotherGoodResult:unwrap()

Functions

ok

Result.ok(selfResult<T,E>) → Option<T>

Converts from Result<T, E> to Option<T>.

Converts self into an Option<T>, and discarding the error, if any.

local x: Result<number, string> = Ok(2)
assert(x:ok() == Some(2))

x = Err("Nothing here")
assert(x:ok() == None())

err

Result.err(selfResult<T,E>) → Option<E>

Converts from Result<T, E> to Option<E>.

Converts self into an Option<E>, and discarding the success value, if any.

local x: Result<number, string> = Ok(2)
assert(x:ok() == Some(2))

x = Err("Nothing here")
assert(x:ok() == None())

transpose

Result.transpose(selfResult<Option<T>,E>) → Option<Result<T,E>>

Transposes a Result of an Option into an Option of a Result.

Result:Ok(Option:None) will be mapped to Option:None. Result:Ok(Option:Some(_)) and Result:Err(_) will be mapped to Option:Some(Result:Ok(_)) and Option:Some(Option:Err(_)).

type SomeErr = {}

local x: Result<Option<number>, SomeErr> = Ok(Some(2))
local y: Option<Result<number, SomeErr>> = Some(Ok(2))
assert(x:transpose() == y)

Ok

Result.Ok(valT) → Result<T,E>

Contains the success value.

Err

Result.Err(errE) → Result<T,E>

Contains the error value.

isOk

Result.isOk(selfResult<T,E>) → boolean

Returns true if the result is Result:Ok.

local x: Result<number, string> = Ok(3)
assert(x:isOk())

x = Err("Some error message")
assert(not x:isOk())

isOkAnd

Result.isOkAnd(
selfResult<T,E>,
predicate(valT?) → boolean
) → boolean

Returns true if the result is Result:Ok and the value inside it matches a predicate.

local x: Result<number, string> = Ok(2)
assert(x:isOkAnd(function(x) return x > 1 end))

x = Ok(0)
assert(not x:isOkAnd(function(x) return x > 1 end))

x = Err("hey")
assert(not x:isOkAnd(function(x) return x > 1 end))

isErr

Result.isErr(selfResult<T,E>) → boolean

Returns true if the result is Result:Err.

local x: Result<number, string> = Ok(3)
assert(not x:isErr())

x = Err("Some error message")
assert(x:isErr())

isErrAnd

Result.isErrAnd(
selfResult<T,E>,
predicate(valT?) → boolean
) → boolean

Returns true if the result is Result:Err and the value inside it matches a predicate.

local x: Result<number, string> = Ok(2)
assert(not x:isErrAnd(function(x) return x > 1 end))

x = Err(3)
assert(x:isErrAnd(function(x) return x > 1 end))

x = Err("hey")
assert(not x:isErrAnd(function(x) return x > 1 end))

map

Result.map(
selfResult<T,E>,
op(valT) → U
) → Result<U,E>

Maps a Result<T, E> to Result<U, E> by applying a function to a contained Result:Ok value, leaving an Result:Err value untouched.

This function can be used to compose the results of two functions.

local lines = "1\n2\n3\n4\n"

function parseInt(x: string): Result<number, string>
	local num = tonumber(x)

	if num == nil then
		return Err("not an integer")
	end

	return Ok(num)
end

for line in lines:split("\n") do
	local number = parseInt(line)

	if number:isOk() then
		print(number:unwrap())
	else
		print("not a number!")
	end
end

mapOr

Result.mapOr(
selfResult<T,E>,
defaultU,
op(valT) → U
) → U

Returns the provided default (if Result:Err), or applies a function to the contained value (if Result:Ok).

Arguments passed to Result:mapOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use Result:mapOrElse, which is lazily evaluated.

local x: Result<string, string> = Ok("foo")
assert(x:mapOr("bar", string.upper) == "FOO")

x = Err("foo")
assert(x:mapOr("bar", string.upper) == "bar")

mapOrElse

Result.mapOrElse(
selfResult<T,E>,
default(valE) → U,
op(valT) → U
) → U

Maps a Result<T, E> to U by applying fallback function default to a contained Result:Err value, or function f to a contained Result:Ok value.

This function can be used to unpack a successful result while handling an error.

local x: Result<string, string> = Ok("foo")
assert(x:mapOrElse(function(x) return "error: ".. x end, string.upper) == "FOO")

x = Err("foo")
assert(x:mapOrElse(function(x) return "error: ".. x end, string.upper) == "error: foo")

mapErr

Result.mapErr(
selfResult<T,E>,
op(valE) → F
) → Result<T,F>

Maps a Result<T, E> to Result<T, F> by applying a function to a contained Result:Err value, leaving an Result:Ok value untouched.

This function can be used to pass through a successful result while handling an error.

local function stringify(x: number): string
	return string.format("error code: %d", x)
end

local x: Result<number, number> = Ok(2)
assert(x:mapErr(stringify) == Ok(2))

x = Err(13)
assert(x:mapErr(stringify) == Err("error code: 13"))

inspect

Result.inspect(
selfResult<T,E>,
op(valE) → ()
) → Result<T,E>

Calls the provided closure with a reference to the contained value (if Result:Ok).

function parseInt(x: string): Result<number, string>
	local num = tonumber(x)

	if num == nil then
		return Err("not an integer")
	end

	return Ok(num)
end

local x = parseInt("4")
	:inspect(function(x) print("original: " .. x) end)
	:map(function(x) return x ^ 3 end)
	:expect("not an integer")

inspectErr

Result.inspectErr(
selfResult<T,E>,
op(valE) → ()
) → Result<T,E>

Calls the provided closure with a reference to the contained value (if Result:Err).

function parseInt(x: string): Result<number, string>
	local num = tonumber(x)

	if num == nil then
		return Err("not an integer")
	end

	return Ok(num)
end

local x = parseInt("string")
	:inspectErr(function(x) print("error: " .. x) end)
	:map(function(x) return x ^ 3 end)
	:unwrap()

expect

Result.expect(
selfResult<T,E>,
msgstring
) → T | never

Returns the contained Result:Ok value, consuming the self value.

Because this function may panic, its use is generally discouraged. Instead, prefer to use Result:isErr and handle the Err case explicitly, or call Result:unwrapOr, Result:unwrapOrElse, or Result:unwrapOrDefault. Panics

local x: Result<number, string> = Err("emergency failure")
x:expect("Testing expect") -- panics with message `Testing expect: emergency failure`

Recommended Message Style

It is recommended that expect messages are used to describe the reason you expect the Result should be Result:Ok.

local process = require("@lune/process")

local function envVar<T>(var: string): Result<T, string>
	local val = process.env[var]

	if val == nil then
		return Err("environment variable not found")
	end

	Ok(val)
end

local path = envVar("IMPORTANT_PATH")
	:expect("env variable `IMPORTANT_PATH` should be set by `wrapper_script.sh`")

Hint: If you’re having trouble remembering how to phrase expect error messages remember to focus on the word “should” as in “env variable should be set by blah” or “the given binary should be available and executable by the current user”.

Errors

TypeDescription
panicIf the value is a [Result:Err], with a panic message including the passed message, and the content of the `Err`.

unwrap

Result.unwrap(selfResult<T,E>) → T | never

Returns the Result:Ok.

Because this function may panic, its use is generally discouraged. Instead, prefer to use Result:unwrapOr, Result:unwrapOrElse, or Result:unwrapOrDefault.

local x: Result<number, string> = Ok(2)
assert(x:unwrap() == 2)

x = Err("oh no")
x:unwrap() -- panics with `oh no`

Errors

TypeDescription
panicPanics if the value is an [Result:Err], with a panic message provided by the [Result:Err]’s value.

expectErr

Result.expectErr(
selfResult<T,E>,
msgstring
) → E | never

Returns the contained Result:Err.

local x: Result<number, string> = Ok(10)
x:expectErr("Testing expect") -- panics with `Testing expect: 10`

Errors

TypeDescription
panicPanics if the value is an [Result:Ok], with a panic message including the passed message, and the content of the [Resul:Ok].

unwrapErr

Result.unwrapErr(selfResult<T,E>) → E | never

Returns the contained Result:Err.

local x: Result<number, string> = Ok(2)
x:unwrapErr() -- panics with `2`
x = Err("oh no")
assert(x:unwrapErr() == "oh no")

Errors

TypeDescription
panicPanics if the value is an [Result:Ok], with a panic message provided by the [Result:Ok]’s value.

and_

Result.and_(
selfResult<T,E>,
resResult<U,E>
) → Result<U,E>

Returns res if the result is Result:Ok, otherwise returns the Result:Err value of self.

local x: Result<number, string> = Ok(2)
local y: Result<string, string> = Err("late error")
assert(x:and_(y) == Err("late error"))

local x: Result<number, string> = Err("early error")
local y: Result<string, string> = Ok("foo")
assert(x:and_(y) == Err("early error"))

local x: Result<number, string> = Err("not a 2")
local y: Result<string, string> = Err("late error")
assert(x:and_(y) == Err("not a 2"))

local x: Result<number, string> = Ok(2)
local y: Result<string, string> = Ok("different result type")
assert(x:and_(y) == Ok("different result type"))

andThen

Result.andThen(
selfResult<T,E>,
op(...any) → any
) → Result<U,E>

Calls op if the result is Result:Ok, otherwise returns the Result:Err value of self.

This function can be used for control flow based on Result values.

function sqThenToString(x): Result<string, string>
	if typeof(sq) ~= "number" then
		return Err("not a number: '" .. x .. "'")
	end

	return Ok(x ^ 2):map(function(sq)
		return tostring(sq)
	end)
end

assert(Ok(2):andThen(sqThenToString) == Ok("4"))
assert(Err("string"):andThen(sqThenToString) == Err("not a number: 'string'"))

Often used to chain fallible operations that may return Result:Err.

or_

Result.or_(
selfResult<T,E>,
resResult<T,F>
) → Result<T,F> | never

Calls op if the result is Result:Ok, otherwise returns the Result:Err value of self.

This function can be used for control flow based on Result values.

local x: Result<number, string> = Ok(2)
local y: Result<number, string> = Err("late error")
assert(x:or_(y) == Ok(2))

local x: Result<number, string> = Err("early error") 
local y: Result<number, string> = Ok(2)
assert(x:or_(y) == Ok(2))

local x: Result<number, string> = Err("not a 2")
local y: Result<number, string> = Err("late error")
assert(x:or_(y) == Err("late error"))

local x: Result<number, string> = Ok(2)
local y: Result<number, string> = Ok(100)
assert(x:or_(y) == Ok(2))

orElse

Result.orElse(
selfResult<T,E>,
op(xE) → Result<T,U>
) → Result<T,U>

Calls op if the result is Result:Ok, otherwise returns the Result:Err value of self.

This function can be used for control flow based on Result values.

function sq(x: number): Result<number, number>
	return Ok(x * x)
end

function err(x: number): Result<number, number>
	return Err(x)
end

assert(Ok(2):orElse(sq):orElse(sq) == Ok(2))
assert(Ok(2):orElse(err):orElse(sq) == Ok(2))
assert(Err(3):orElse(sq):orElse(err) == Ok(9))
assert(Err(3):orElse(err):orElse(err) == Err(3))

unwrapOr

Result.unwrapOr(
selfResult<T,E>,
defaultT
) → T

Returns the contained Result:Ok value or a provided default.

Arguments passed to Result:unwrapOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use Result:unwrapOrElse, which is lazily evaluated.

local default = 2
local x: Result<number, string> = Ok(9)
assert(x:unwrapOr(default), 9)

x = Err("error")
assert(x:unwrapOr(default), default)

unwrapOrElse

Result.unwrapOrElse(
selfResult<T,E>,
op(xE) → T
) → T

Returns the contained Result:Ok value or computes it from a closure.

function count(x: string): number
	return #x
end

assert(Ok(2):unwrapOrElse(count) == 2))
assert(Err("foo"):unwrapOrElse(count) == 3))

contains

Result.contains(
selfResult<T,E>,
valT
) → boolean

Returns true if the Result:Ok value is val.

local x: Result<number, string> = Ok(2)
assert(x:contains(2))

x = Ok(3)
assert(not x:contains(2))

x = Err(2)
assert(not x:contains(2))

containsErr

Result.containsErr(
selfResult<T,E>,
errE
) → boolean

Returns true if the Result:Err value is err.

local x: Result<number, string> = Err(2)
assert(x:containsErr(2))

x = Err(3)
assert(not x:containsErr(2))

x = Ok(2)
assert(not x:containsErr(2))

display

Result.display(selfResult<T,E>) → string

Returns a formatted representation of the result, often used for printing to stdout.

local x: Result<number, string> = Ok(123)
local y: Result<number, string> = Err("error")

print(x:display()) -- prints `Result::Ok(123)`
print(y:display()) -- prints `Result::Err("error")`
Show raw api
{
    "functions": [
        {
            "name": "ok",
            "desc": "Converts from [Result]`<T, E>` to [Option]`<T>`.\n\nConverts `self` into an [Option]`<T>`, and discarding the error, if any.\n\n```lua\nlocal x: Result<number, string> = Ok(2)\nassert(x:ok() == Some(2))\n\nx = Err(\"Nothing here\")\nassert(x:ok() == None())\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Option<T>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 40,
                "path": "lib/conversion.luau"
            }
        },
        {
            "name": "err",
            "desc": "Converts from [Result]`<T, E>` to [Option]`<E>`.\n\nConverts `self` into an [Option]`<E>`, and discarding the success value, if any.\n\n```lua\nlocal x: Result<number, string> = Ok(2)\nassert(x:ok() == Some(2))\n\nx = Err(\"Nothing here\")\nassert(x:ok() == None())\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Option<E>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 70,
                "path": "lib/conversion.luau"
            }
        },
        {
            "name": "transpose",
            "desc": "Transposes a [Result] of an [Option] into an [Option] of a [Result].\n\n[Result:Ok]\\([Option:None]\\) will be mapped to [Option:None]. \n[Result:Ok]\\([Option:Some]`(_)`\\) and [Result:Err]\\(`_`\\) will be mapped to \n[Option:Some]\\([Result:Ok]`(_)`\\) and [Option:Some]\\([Option:Err]`(_)`\\).\n\n```lua\ntype SomeErr = {}\n\nlocal x: Result<Option<number>, SomeErr> = Ok(Some(2))\nlocal y: Option<Result<number, SomeErr>> = Some(Ok(2))\nassert(x:transpose() == y)\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<Option<T>, E>"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Option<Result<T, E>>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 98,
                "path": "lib/conversion.luau"
            }
        },
        {
            "name": "Ok",
            "desc": "Contains the success value.",
            "params": [
                {
                    "name": "val",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Result<T, E>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 82,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "Err",
            "desc": "Contains the error value.",
            "params": [
                {
                    "name": "err",
                    "desc": "",
                    "lua_type": "E"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Result<T, E>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 94,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "new",
            "desc": "Constructs a [Result]. **For internal use only**.\n\nFor user consumption, see [Result:Ok] and [Result:Err].",
            "params": [
                {
                    "name": "val",
                    "desc": "",
                    "lua_type": "T"
                },
                {
                    "name": "err",
                    "desc": "",
                    "lua_type": "E"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Result<T, E>"
                }
            ],
            "function_type": "static",
            "private": true,
            "source": {
                "line": 109,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "isOk",
            "desc": "Returns `true` if the result is [Result:Ok].\n\n```lua\nlocal x: Result<number, string> = Ok(3)\nassert(x:isOk())\n\nx = Err(\"Some error message\")\nassert(not x:isOk())\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 164,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "isOkAnd",
            "desc": "Returns `true` if the result is [Result:Ok] and the value inside it matches a \npredicate.\n\n```lua\nlocal x: Result<number, string> = Ok(2)\nassert(x:isOkAnd(function(x) return x > 1 end))\n\nx = Ok(0)\nassert(not x:isOkAnd(function(x) return x > 1 end))\n\nx = Err(\"hey\")\nassert(not x:isOkAnd(function(x) return x > 1 end))\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "predicate",
                    "desc": "",
                    "lua_type": "(val: T?) -> boolean"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 193,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "isErr",
            "desc": "Returns `true` if the result is [Result:Err].\n\n```lua\nlocal x: Result<number, string> = Ok(3)\nassert(not x:isErr())\n\nx = Err(\"Some error message\")\nassert(x:isErr())\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 213,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "isErrAnd",
            "desc": "Returns `true` if the result is [Result:Err] and the value inside it matches a \npredicate.\n\n```lua\nlocal x: Result<number, string> = Ok(2)\nassert(not x:isErrAnd(function(x) return x > 1 end))\n\nx = Err(3)\nassert(x:isErrAnd(function(x) return x > 1 end))\n\nx = Err(\"hey\")\nassert(not x:isErrAnd(function(x) return x > 1 end))\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "predicate",
                    "desc": "",
                    "lua_type": "(val: T?) -> boolean"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 238,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "map",
            "desc": "Maps a [Result]<T, E> to [Result]<U, E> by applying a function to a contained [Result:Ok] value, \nleaving an [Result:Err] value untouched.\n\nThis function can be used to compose the results of two functions.\n\n```lua\nlocal lines = \"1\\n2\\n3\\n4\\n\"\n\nfunction parseInt(x: string): Result<number, string>\n\tlocal num = tonumber(x)\n\n\tif num == nil then\n\t\treturn Err(\"not an integer\")\n\tend\n\n\treturn Ok(num)\nend\n\nfor line in lines:split(\"\\n\") do\n\tlocal number = parseInt(line)\n\n\tif number:isOk() then\n\t\tprint(number:unwrap())\n\telse\n\t\tprint(\"not a number!\")\n\tend\nend\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "op",
                    "desc": "",
                    "lua_type": "(val: T) -> U"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Result<U, E>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 282,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "mapOr",
            "desc": "Returns the provided default (if [Result:Err]), or applies a function to \nthe contained value (if [Result:Ok]).\n\nArguments passed to [Result:mapOr] are eagerly evaluated; if you are passing the \nresult of a function call, it is recommended to use [Result:mapOrElse], which is \nlazily evaluated.\n\n```lua\nlocal x: Result<string, string> = Ok(\"foo\")\nassert(x:mapOr(\"bar\", string.upper) == \"FOO\")\n\nx = Err(\"foo\")\nassert(x:mapOr(\"bar\", string.upper) == \"bar\")\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "default",
                    "desc": "",
                    "lua_type": "U"
                },
                {
                    "name": "op",
                    "desc": "",
                    "lua_type": "(val: T) -> U"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "U"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 313,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "mapOrElse",
            "desc": "Maps a [Result]<T, E> to U by applying fallback function default to a contained\n[Result:Err] value, or function f to a contained [Result:Ok] value.\n\nThis function can be used to unpack a successful result while handling an error.\n\n```lua\nlocal x: Result<string, string> = Ok(\"foo\")\nassert(x:mapOrElse(function(x) return \"error: \".. x end, string.upper) == \"FOO\")\n\nx = Err(\"foo\")\nassert(x:mapOrElse(function(x) return \"error: \".. x end, string.upper) == \"error: foo\")\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "default",
                    "desc": "",
                    "lua_type": "(val: E) -> U"
                },
                {
                    "name": "op",
                    "desc": "",
                    "lua_type": "(val: T) -> U"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "U"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 342,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "mapErr",
            "desc": "Maps a [Result]<T, E> to [Result]<T, F> by applying a function to a contained \n[Result:Err] value, leaving an [Result:Ok] value untouched.\n\nThis function can be used to pass through a successful result while handling an\nerror.\n\n```lua\nlocal function stringify(x: number): string\n\treturn string.format(\"error code: %d\", x)\nend\n\nlocal x: Result<number, number> = Ok(2)\nassert(x:mapErr(stringify) == Ok(2))\n\nx = Err(13)\nassert(x:mapErr(stringify) == Err(\"error code: 13\"))\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "op",
                    "desc": "",
                    "lua_type": "(val: E) -> F"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Result<T, F>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 375,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "inspect",
            "desc": "Calls the provided closure with a reference to the contained value \n(if [Result:Ok]).\n\n```lua\nfunction parseInt(x: string): Result<number, string>\n\tlocal num = tonumber(x)\n\n\tif num == nil then\n\t\treturn Err(\"not an integer\")\n\tend\n\n\treturn Ok(num)\nend\n\nlocal x = parseInt(\"4\")\n\t:inspect(function(x) print(\"original: \" .. x) end)\n\t:map(function(x) return x ^ 3 end)\n\t:expect(\"not an integer\")\n\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "op",
                    "desc": "",
                    "lua_type": "(val: E) -> ()"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Result<T, E>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 411,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "inspectErr",
            "desc": "Calls the provided closure with a reference to the contained value \n(if [Result:Err]).\n\n```lua\nfunction parseInt(x: string): Result<number, string>\n\tlocal num = tonumber(x)\n\n\tif num == nil then\n\t\treturn Err(\"not an integer\")\n\tend\n\n\treturn Ok(num)\nend\n\nlocal x = parseInt(\"string\")\n\t:inspectErr(function(x) print(\"error: \" .. x) end)\n\t:map(function(x) return x ^ 3 end)\n\t:unwrap()\n\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "op",
                    "desc": "",
                    "lua_type": "(val: E) -> ()"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Result<T, E>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 447,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "expect",
            "desc": "Returns the contained [Result:Ok] value, consuming the self value.\n\nBecause this function may panic, its use is generally discouraged. Instead, \nprefer to use [Result:isErr] and handle the Err case explicitly, or call \n[Result:unwrapOr], [Result:unwrapOrElse], or [Result:unwrapOrDefault].\nPanics\n\n\n```lua\nlocal x: Result<number, string> = Err(\"emergency failure\")\nx:expect(\"Testing expect\") -- panics with message `Testing expect: emergency failure`\n```\n\n## Recommended Message Style\nIt is recommended that expect messages are used to describe the reason you expect the \n[Result] should be [Result:Ok].\n\n```lua\nlocal process = require(\"@lune/process\")\n\nlocal function envVar<T>(var: string): Result<T, string>\n\tlocal val = process.env[var]\n\n\tif val == nil then\n\t\treturn Err(\"environment variable not found\")\n\tend\n\n\tOk(val)\nend\n\nlocal path = envVar(\"IMPORTANT_PATH\")\n\t:expect(\"env variable `IMPORTANT_PATH` should be set by `wrapper_script.sh`\")\n```\n\n**Hint**: If you’re having trouble remembering how to phrase expect error messages remember to focus \non the word “should” as in “env variable should be set by blah” or “the given binary should be available \nand executable by the current user”.",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "msg",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "T | never"
                }
            ],
            "function_type": "static",
            "errors": [
                {
                    "lua_type": "panic",
                    "desc": "If the value is a [Result:Err], with a panic message including the passed message, and the content of the `Err`."
                }
            ],
            "source": {
                "line": 507,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "unwrap",
            "desc": "Returns the [Result:Ok].\n\nBecause this function may panic, its use is generally discouraged. Instead, prefer to use \n[Result:unwrapOr], [Result:unwrapOrElse], or [Result:unwrapOrDefault].\n\n\n```lua\nlocal x: Result<number, string> = Ok(2)\nassert(x:unwrap() == 2)\n\nx = Err(\"oh no\")\nx:unwrap() -- panics with `oh no`\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "T | never\n"
                }
            ],
            "function_type": "static",
            "errors": [
                {
                    "lua_type": "panic",
                    "desc": "Panics if the value is an [Result:Err], with a panic message provided by the [Result:Err]’s value."
                }
            ],
            "source": {
                "line": 535,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "expectErr",
            "desc": "Returns the contained [Result:Err].\n\n\n```lua\nlocal x: Result<number, string> = Ok(10)\nx:expectErr(\"Testing expect\") -- panics with `Testing expect: 10`\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "msg",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "E | never"
                }
            ],
            "function_type": "static",
            "errors": [
                {
                    "lua_type": "panic",
                    "desc": "Panics if the value is an [Result:Ok], with a panic message including the passed message, and the content of the [Resul:Ok]."
                }
            ],
            "source": {
                "line": 565,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "unwrapErr",
            "desc": "Returns the contained [Result:Err].\n\n\n```lua\nlocal x: Result<number, string> = Ok(2)\nx:unwrapErr() -- panics with `2`\n```\n```lua\nx = Err(\"oh no\")\nassert(x:unwrapErr() == \"oh no\")\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "E | never"
                }
            ],
            "function_type": "static",
            "errors": [
                {
                    "lua_type": "panic",
                    "desc": "Panics if the value is an [Result:Ok], with a panic message provided by the [Result:Ok]’s value."
                }
            ],
            "source": {
                "line": 592,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "and_",
            "desc": "Returns res if the result is [Result:Ok], otherwise returns the \n[Result:Err] value of self.\n\n```lua\nlocal x: Result<number, string> = Ok(2)\nlocal y: Result<string, string> = Err(\"late error\")\nassert(x:and_(y) == Err(\"late error\"))\n\nlocal x: Result<number, string> = Err(\"early error\")\nlocal y: Result<string, string> = Ok(\"foo\")\nassert(x:and_(y) == Err(\"early error\"))\n\nlocal x: Result<number, string> = Err(\"not a 2\")\nlocal y: Result<string, string> = Err(\"late error\")\nassert(x:and_(y) == Err(\"not a 2\"))\n\nlocal x: Result<number, string> = Ok(2)\nlocal y: Result<string, string> = Ok(\"different result type\")\nassert(x:and_(y) == Ok(\"different result type\"))\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "res",
                    "desc": "",
                    "lua_type": "Result<U, E>"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Result<U, E>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 638,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "andThen",
            "desc": "Calls `op` if the result is [Result:Ok], otherwise returns the \n[Result:Err] value of `self`.\n\nThis function can be used for control flow based on [Result] \nvalues.\n\n```lua\nfunction sqThenToString(x): Result<string, string>\n\tif typeof(sq) ~= \"number\" then\n\t\treturn Err(\"not a number: '\" .. x .. \"'\")\n\tend\n\n\treturn Ok(x ^ 2):map(function(sq)\n\t\treturn tostring(sq)\n\tend)\nend\n\nassert(Ok(2):andThen(sqThenToString) == Ok(\"4\"))\nassert(Err(\"string\"):andThen(sqThenToString) == Err(\"not a number: 'string'\"))\n```\n\nOften used to chain fallible operations that may return [Result:Err].",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "op",
                    "desc": "",
                    "lua_type": "(...any) -> any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Result<U, E>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 676,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "or_",
            "desc": "Calls `op` if the result is [Result:Ok], otherwise returns the \n[Result:Err] value of `self`.\n\nThis function can be used for control flow based on [Result] \nvalues.\n\n```lua\nlocal x: Result<number, string> = Ok(2)\nlocal y: Result<number, string> = Err(\"late error\")\nassert(x:or_(y) == Ok(2))\n\nlocal x: Result<number, string> = Err(\"early error\") \nlocal y: Result<number, string> = Ok(2)\nassert(x:or_(y) == Ok(2))\n\nlocal x: Result<number, string> = Err(\"not a 2\")\nlocal y: Result<number, string> = Err(\"late error\")\nassert(x:or_(y) == Err(\"late error\"))\n\nlocal x: Result<number, string> = Ok(2)\nlocal y: Result<number, string> = Ok(100)\nassert(x:or_(y) == Ok(2))\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "res",
                    "desc": "",
                    "lua_type": "Result<T, F>"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Result<T, F> | never"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 715,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "orElse",
            "desc": "Calls `op` if the result is [Result:Ok], otherwise returns the \n[Result:Err] value of `self`.\n\nThis function can be used for control flow based on [Result] \nvalues.\n\n```lua\nfunction sq(x: number): Result<number, number>\n\treturn Ok(x * x)\nend\n\nfunction err(x: number): Result<number, number>\n\treturn Err(x)\nend\n\nassert(Ok(2):orElse(sq):orElse(sq) == Ok(2))\nassert(Ok(2):orElse(err):orElse(sq) == Ok(2))\nassert(Err(3):orElse(sq):orElse(err) == Ok(9))\nassert(Err(3):orElse(err):orElse(err) == Err(3))\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "op",
                    "desc": "",
                    "lua_type": "(x: E) -> Result<T, U>"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Result<T, U>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 755,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "unwrapOr",
            "desc": "Returns the contained [Result:Ok] value or a provided default.\n\nArguments passed to [Result:unwrapOr] are eagerly evaluated; if you are passing the \nresult of a function call, it is recommended to use [Result:unwrapOrElse], which is \nlazily evaluated.\n\n```lua\nlocal default = 2\nlocal x: Result<number, string> = Ok(9)\nassert(x:unwrapOr(default), 9)\n\nx = Err(\"error\")\nassert(x:unwrapOr(default), default)\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "default",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 785,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "unwrapOrElse",
            "desc": "Returns the contained [Result:Ok] value or computes it from a closure.\n\n```lua\nfunction count(x: string): number\n\treturn #x\nend\n\nassert(Ok(2):unwrapOrElse(count) == 2))\nassert(Err(\"foo\"):unwrapOrElse(count) == 3))\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "op",
                    "desc": "",
                    "lua_type": "(x: E) -> T"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 811,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "contains",
            "desc": "Returns true if the [Result:Ok] value is `val`.\n\n```lua\nlocal x: Result<number, string> = Ok(2)\nassert(x:contains(2))\n\nx = Ok(3)\nassert(not x:contains(2))\n\nx = Err(2)\nassert(not x:contains(2))\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "val",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 839,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "containsErr",
            "desc": "Returns true if the [Result:Err] value is `err`.\n\n```lua\nlocal x: Result<number, string> = Err(2)\nassert(x:containsErr(2))\n\nx = Err(3)\nassert(not x:containsErr(2))\n\nx = Ok(2)\nassert(not x:containsErr(2))\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                },
                {
                    "name": "err",
                    "desc": "",
                    "lua_type": "E"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 867,
                "path": "lib/result.luau"
            }
        },
        {
            "name": "display",
            "desc": "Returns a formatted representation of the result, often\nused for printing to stdout.\n\n```lua\nlocal x: Result<number, string> = Ok(123)\nlocal y: Result<number, string> = Err(\"error\")\n\nprint(x:display()) -- prints `Result::Ok(123)`\nprint(y:display()) -- prints `Result::Err(\"error\")`\n```",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "Result<T, E>"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 892,
                "path": "lib/result.luau"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "Result",
    "desc": "Error handling with the [Result] type.\n\n[Result]`<T, E>` is the type used for returning a possible error. It is a class with the \nconstructor variants, [Result:Ok]`(T)`, representing success and containing a value, and \n[Result:Err]`(E)`, representing error and containing an error value.\n\n```lua\nlocal result: Result<number, string>\n\n-- Both variants `Ok` and `Err` fit into the type\nresult = Ok(21)\nresult = Err(\"some error\")\n```\n\nFunctions return [Result] whenever errors are expected and recoverable.\n\nA simple function returning [Result] might be defined and used like so:\n```lua\nlocal PAT = \"v(%d+)\"\n\nfunction parseVersion(str: string): Result<number, string>\n\tlocal version: number = string.match(str, PAT)\n\n\tif version == 0 then\n\t\treturn Err(\"Version cannot be zero\")\n\telse if version == nil then\n\t\treturn Err(\"Invalid string\")\n\tend\n\n\treturn Ok(version)\nend\n\nprint(\"Successful result: \" .. parseVersion(\"v12\"):display())\nprint(\"Error result: \" .. parseVersion(\"vString\"):display())\n```\n\n[Result] comes with some convenience methods that make working with it more succinct.\n\n```lua\nlocal goodResult: Result<number, number> = Ok(10)\nlocal badResult: Result<number, number> = Err(10)\n\n-- The `isOk`and `isErr` methods do what they say.\nassert(goodResult:isOk() and not goodResult:isErr())\nassert(badResult:isErr() and not badResult:isOk())\n\n-- `map` produces a new result from an existing one \ngoodResult = goodResult:map(function(i) return i + 1 end)\nbadResult = badResult:map(function(i) return i - 1 end)\n\n-- Use `and_then` to continue the computation.\nlocal anotherGoodResult: Result<boolean, number> = goodResult:andThen(function(i) return Ok(i == 11) end)\n\n-- Use `or_else` to handle the error\nlocal badResult: Result<number, number> = bad_result:orElse(function(i) return Ok(i + 20) end)\n\n-- Get the internal `Ok` value and panic if there is an `Err`\nlocal finalAwesomeResult: boolean = anotherGoodResult:unwrap()\n```",
    "source": {
        "line": 66,
        "path": "lib/result.luau"
    }
}