#
cp.result
Represents the result of an operation which may end in success
or failure
.
If it is a success
, a value
is typically provided.
If it is a failure
, a message
is typically provided.
Using this type allows for more structured checking when performing an operation which may fail in a number
of ways, rather than just calling error
and crashing out. For example:
function clamped(value, min, max)
if value < min then
return result.failure("expected at least %d but got %d", min, value)
elseif value > max then
return result.failure("expected at most %d but got %d", max, value")
else
return result.success(value)
end
end
local outcome = clamped(-1, 0, 100)
if outcome.failure then
error(outcome.message)
end
local value = outcome.value
Of course, simply checking the result and throwing an error
is a common case, so you can achieve the same result like so:
local value = clamped(-1, 0, 100):get()
If you want to perform other tasks, check for .failure
or .success
and perform the appropriate response.
#
API Overview
Functions - API calls offered directly by the extension
is
Constructors - API calls which return an object, typically one that offers API methods
failure from okValue success valueErr
Methods - API calls which can only be made on an object returned by a constructor
get log