Skip to main content

Option

Represents an optional value in Lua. This is useful to avoid nil bugs, which can go silently undetected within code and cause hidden or hard-to-find bugs.

Properties

None

Option.None: Option<None>

Represents no value.

Functions

Some

Option.Some(valueT) → Option<T>

Creates an Option instance with the given value. Throws an error if the given value is nil.

Wrap

Option.Wrap(valueT) → Option<T> | Option<None>

Safely wraps the given value as an option. If the value is nil, returns Option.None, otherwise returns Option.Some(value).

Is

Option.Is(objany) → boolean

Returns true if obj is an Option.

Assert

Option.Assert(objany) → ()

Throws an error if obj is not an Option.

Deserialize

Option.Deserialize(datatable) → Option

Deserializes the data into an Option. This data should have come from the option:Serialize() method.

Serialize

Option:Serialize() → table

Returns a serialized version of the option.

Match

Option:Match(matches{
Some(valueany) → any,
None() → any
}) → any

Matches against the option.

local opt = Option.Some(32)
opt:Match {
	Some = function(num) print("Number", num) end,
	None = function() print("No value") end,
}

IsSome

Option:IsSome() → boolean

Returns true if the option has a value.

IsNone

Option:IsNone() → boolean

Returns true if the option is None.

Expect

Option:Expect(msgstring) → valueany

Unwraps the value in the option, otherwise throws an error with msg as the error message.

local opt = Option.Some(10)
print(opt:Expect("No number")) -> 10
print(Option.None:Expect("No number")) -- Throws an error "No number"

ExpectNone

Option:ExpectNone(msgstring) → ()

Throws an error with msg as the error message if the value is not None.

Unwrap

Option:Unwrap() → valueany

Returns the value in the option, or throws an error if the option is None.

UnwrapOr

Option:UnwrapOr(defaultany) → valueany

If the option holds a value, returns the value. Otherwise, returns default.

UnwrapOrElse

Option:UnwrapOrElse(defaultFn() → any) → valueany

If the option holds a value, returns the value. Otherwise, returns the result of the defaultFn function.

And

Option:And(optionBOption) → Option

Returns optionB if the calling option has a value, otherwise returns None.

local optionA = Option.Some(32)
local optionB = Option.Some(64)
local opt = optionA:And(optionB)
-- opt == optionB

local optionA = Option.None
local optionB = Option.Some(64)
local opt = optionA:And(optionB)
-- opt == Option.None

AndThen

Option:AndThen(andThenFn(valueany) → Option) → valueOption

If the option holds a value, then the andThenFn function is called with the held value of the option, and then the resultant Option returned by the andThenFn is returned. Otherwise, None is returned.

local optA = Option.Some(32)
local optB = optA:AndThen(function(num)
	return Option.Some(num * 2)
end)
print(optB:Expect("Expected number")) --> 64

Or

Option:Or(optionBOption) → Option

If caller has a value, returns itself. Otherwise, returns optionB.

OrElse

Option:OrElse(orElseFn() → Option) → Option

If caller has a value, returns itself. Otherwise, returns the option generated by the orElseFn function.

XOr

Option:XOr(optionBOption) → Option

If both self and optionB have values or both don't have a value, then this returns None. Otherwise, it returns the option that does have a value.

Filter

Option:Filter(predicate(valueany) → boolean) → Option

Returns self if this option has a value and the predicate returns `true. Otherwise, returns None.

Contains

Option:Contains(valueany) → boolean

Returns true if this option contains value.

__tostring

Option:__tostring() → string

Metamethod to transform the option into a string.

local optA = Option.Some(64)
local optB = Option.None
print(optA) --> Option<number>
print(optB) --> Option<None>

__eq

Option:__eq(optOption) → boolean

Metamethod to check equality between two options. Returns true if both options hold the same value or both options are None.

local o1 = Option.Some(32)
local o2 = Option.Some(32)
local o3 = Option.Some(64)
local o4 = Option.None
local o5 = Option.None

print(o1 == o2) --> true
print(o1 == o3) --> false
print(o1 == o4) --> false
print(o4 == o5) --> true
Show raw api
{
    "functions": [
        {
            "name": "Some",
            "desc": "Creates an Option instance with the given value. Throws an error\nif the given value is `nil`.",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Option<T>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 145,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "Wrap",
            "desc": "Safely wraps the given value as an option. If the\nvalue is `nil`, returns `Option.None`, otherwise\nreturns `Option.Some(value)`.",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Option<T> | Option<None>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 158,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "Is",
            "desc": "Returns `true` if `obj` is an Option.",
            "params": [
                {
                    "name": "obj",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 171,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "Assert",
            "desc": "Throws an error if `obj` is not an Option.",
            "params": [
                {
                    "name": "obj",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 179,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "Deserialize",
            "desc": "Deserializes the data into an Option. This data should have come from\nthe `option:Serialize()` method.",
            "params": [
                {
                    "name": "data",
                    "desc": "",
                    "lua_type": "table"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Option"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 189,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "Serialize",
            "desc": "Returns a serialized version of the option.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "table"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 198,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "Match",
            "desc": "Matches against the option.\n\n```lua\nlocal opt = Option.Some(32)\nopt:Match {\n\tSome = function(num) print(\"Number\", num) end,\n\tNone = function() print(\"No value\") end,\n}\n```",
            "params": [
                {
                    "name": "matches",
                    "desc": "",
                    "lua_type": "{Some: (value: any) -> any, None: () -> any}"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 219,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "IsSome",
            "desc": "Returns `true` if the option has a value.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 235,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "IsNone",
            "desc": "Returns `true` if the option is None.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 243,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "Expect",
            "desc": "Unwraps the value in the option, otherwise throws an error with `msg` as the error message.\n```lua\nlocal opt = Option.Some(10)\nprint(opt:Expect(\"No number\")) -> 10\nprint(Option.None:Expect(\"No number\")) -- Throws an error \"No number\"\n```",
            "params": [
                {
                    "name": "msg",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "value: any"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 257,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "ExpectNone",
            "desc": "Throws an error with `msg` as the error message if the value is _not_ None.",
            "params": [
                {
                    "name": "msg",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 266,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "Unwrap",
            "desc": "Returns the value in the option, or throws an error if the option is None.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "value: any"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 274,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "UnwrapOr",
            "desc": "If the option holds a value, returns the value. Otherwise, returns `default`.",
            "params": [
                {
                    "name": "default",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "value: any"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 283,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "UnwrapOrElse",
            "desc": "If the option holds a value, returns the value. Otherwise, returns the\nresult of the `defaultFn` function.",
            "params": [
                {
                    "name": "defaultFn",
                    "desc": "",
                    "lua_type": "() -> any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "value: any"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 297,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "And",
            "desc": "Returns `optionB` if the calling option has a value,\notherwise returns None.\n\n```lua\nlocal optionA = Option.Some(32)\nlocal optionB = Option.Some(64)\nlocal opt = optionA:And(optionB)\n-- opt == optionB\n\nlocal optionA = Option.None\nlocal optionB = Option.Some(64)\nlocal opt = optionA:And(optionB)\n-- opt == Option.None\n```",
            "params": [
                {
                    "name": "optionB",
                    "desc": "",
                    "lua_type": "Option"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Option"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 323,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "AndThen",
            "desc": "If the option holds a value, then the `andThenFn`\nfunction is called with the held value of the option,\nand then the resultant Option returned by the `andThenFn`\nis returned. Otherwise, None is returned.\n\n```lua\nlocal optA = Option.Some(32)\nlocal optB = optA:AndThen(function(num)\n\treturn Option.Some(num * 2)\nend)\nprint(optB:Expect(\"Expected number\")) --> 64\n```",
            "params": [
                {
                    "name": "andThenFn",
                    "desc": "",
                    "lua_type": "(value: any) -> Option"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "value: Option"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 347,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "Or",
            "desc": "If caller has a value, returns itself. Otherwise, returns `optionB`.",
            "params": [
                {
                    "name": "optionB",
                    "desc": "",
                    "lua_type": "Option"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Option"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 362,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "OrElse",
            "desc": "If caller has a value, returns itself. Otherwise, returns the\noption generated by the `orElseFn` function.",
            "params": [
                {
                    "name": "orElseFn",
                    "desc": "",
                    "lua_type": "() -> Option"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Option"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 376,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "XOr",
            "desc": "If both `self` and `optionB` have values _or_ both don't have a value,\nthen this returns None. Otherwise, it returns the option that does have\na value.",
            "params": [
                {
                    "name": "optionB",
                    "desc": "",
                    "lua_type": "Option"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Option"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 393,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "Filter",
            "desc": "Returns `self` if this option has a value and the predicate returns `true.\nOtherwise, returns None.",
            "params": [
                {
                    "name": "predicate",
                    "desc": "",
                    "lua_type": "(value: any) -> boolean"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Option"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 411,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "Contains",
            "desc": "Returns `true` if this option contains `value`.",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 424,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "__tostring",
            "desc": "Metamethod to transform the option into a string.\n```lua\nlocal optA = Option.Some(64)\nlocal optB = Option.None\nprint(optA) --> Option<number>\nprint(optB) --> Option<None>\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 438,
                "path": "modules/option/init.lua"
            }
        },
        {
            "name": "__eq",
            "desc": "Metamethod to check equality between two options. Returns `true` if both\noptions hold the same value _or_ both options are None.\n```lua\nlocal o1 = Option.Some(32)\nlocal o2 = Option.Some(32)\nlocal o3 = Option.Some(64)\nlocal o4 = Option.None\nlocal o5 = Option.None\n\nprint(o1 == o2) --> true\nprint(o1 == o3) --> false\nprint(o1 == o4) --> false\nprint(o4 == o5) --> true\n```",
            "params": [
                {
                    "name": "opt",
                    "desc": "",
                    "lua_type": "Option"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 464,
                "path": "modules/option/init.lua"
            }
        }
    ],
    "properties": [
        {
            "name": "None",
            "desc": "Represents no value.",
            "lua_type": "Option<None>",
            "source": {
                "line": 480,
                "path": "modules/option/init.lua"
            }
        }
    ],
    "types": [],
    "name": "Option",
    "desc": "Represents an optional value in Lua. This is useful to avoid `nil` bugs, which can\ngo silently undetected within code and cause hidden or hard-to-find bugs.",
    "source": {
        "line": 126,
        "path": "modules/option/init.lua"
    }
}