diff --git a/examples/russianRoullete.luau b/examples/russianRoullete.luau index 49fec36..bcaf34e 100644 --- a/examples/russianRoullete.luau +++ b/examples/russianRoullete.luau @@ -4,17 +4,17 @@ local Ok = result.Ok local Err = result.Err local function canError(): Result - if math.round(math.random()) == 1 then - return Err("you DIED") - end + if math.round(math.random()) == 1 then + return Err("you DIED") + end - return Ok(69) + return Ok(69) end function main() - local val = canError():unwrap() + local val = canError():unwrap() - print("got value: ", val) + print("got value: ", val) end return main() diff --git a/lib/conversion.luau b/lib/conversion.luau index a55588b..ee648e6 100644 --- a/lib/conversion.luau +++ b/lib/conversion.luau @@ -38,15 +38,15 @@ local Err = result.Err @return Option ]=] function Result.ok(self: Result): Option - if self:isOk() then - if self._value == nil then - return None() - end + if self:isOk() then + if self._value == nil then + return None() + end - return Some(self._value) - end + return Some(self._value) + end - return None() + return None() end --[=[ @@ -68,11 +68,11 @@ end @return Option ]=] function Result.err(self: Result): Option - if self:isErr() then - return Option.new(self._error) :: Option - end + if self:isErr() then + return Option.new(self._error) :: Option + end - return None() + return None() end --[=[ @@ -96,17 +96,19 @@ end @return Option> ]=] function Result.transpose(self: Result, E>): Option> - if self._value == None() then - return None() - elseif self:isOkAnd(function(val): boolean - return val._optValue == nil - end) then - return Some(Ok(self._value._optValue)) - elseif self:isErr() then - return Some(Err(self._error)) - end + if self._value == None() then + return None() + elseif + self:isOkAnd(function(val): boolean + return val._optValue == nil + end) + then + return Some(Ok(self._value._optValue)) + elseif self:isErr() then + return Some(Err(self._error)) + end - error("`Result` is not transposable") + error("`Result` is not transposable") end --[=[ @@ -131,11 +133,11 @@ end @return Result ]=] function Option.okOr(self: Option, err: E): Result - if self:isSome() then - return Ok(self._optValue) - end + if self:isSome() then + return Ok(self._optValue) + end - return Err(err) + return Err(err) end --[=[ @@ -157,11 +159,11 @@ end @return Result ]=] function Option.okOrElse(self: Option, err: () -> E): Result - if self:isSome() then - return Ok(self._optValue :: T) - end + if self:isSome() then + return Ok(self._optValue :: T) + end - return Err(err()) + return Err(err()) end --[=[ @@ -185,26 +187,29 @@ end @return Result, E> ]=] function Option.transpose(self: Option>): Result, E> - if self:isSome() then - local inner = self._optValue - assert(self.typeId == "Option" and inner.typeId == "Result", "Only an `Option` of a `Result` can be transposed") + if self:isSome() then + local inner = self._optValue + assert( + self.typeId == "Option" and inner.typeId == "Result", + "Only an `Option` of a `Result` can be transposed" + ) - if inner:isOk() then - return Some(Ok(inner._value)) - elseif inner:isErr() then - return Some(Err(inner._error)) - end - end + if inner:isOk() then + return Some(Ok(inner._value)) + elseif inner:isErr() then + return Some(Err(inner._error)) + end + end - return Ok(None()) + return Ok(None()) end return { - Ok = Ok, - Err = Err, - Result = Result, + Ok = Ok, + Err = Err, + Result = Result, - Some = Some, - None = None, - Option = Option, + Some = Some, + None = None, + Option = Option, } diff --git a/lib/future.luau b/lib/future.luau index b6d836b..aeeb179 100644 --- a/lib/future.luau +++ b/lib/future.luau @@ -73,26 +73,26 @@ export type Status = "initialized" | "pending" | "cancelled" | "ready" ]=] export type Future = typeof(Future) & { - _ret: T, - _thread: thread, - _spawnEvt: Signal<()>, - _retEvt: Signal, Status>, - _status: Status, + _ret: T, + _thread: thread, + _spawnEvt: Signal<()>, + _retEvt: Signal, Status>, + _status: Status, } local function _constructor(fn: (Signal<()>, Signal) -> ()) - return setmetatable( - { - _thread = coroutine.create(fn), + return setmetatable( + { + _thread = coroutine.create(fn), - _spawnEvt = Signal.new(), - _retEvt = Signal.new(), - _status = "initialized", - } :: Future, - { - __index = Future, - } - ) + _spawnEvt = Signal.new(), + _retEvt = Signal.new(), + _status = "initialized", + } :: Future, + { + __index = Future, + } + ) end --[=[ @@ -110,12 +110,14 @@ end @return Future -- The constructed future ]=] function Future.new(fn: (...any) -> T, args: { any }) - return _constructor(function(spawnEvt: Signal<()>, retEvt: Signal) - spawnEvt:Fire() + return _constructor( + function(spawnEvt: Signal<()>, retEvt: Signal) + spawnEvt:Fire() - local ret = fn(table.unpack(args)) - retEvt:Fire(ret, "ready") - end) + local ret = fn(table.unpack(args)) + retEvt:Fire(ret, "ready") + end + ) end --[=[ @@ -128,13 +130,18 @@ end @return Future> -- The constructed future ]=] function Future.try(fn: (...any) -> T, args: { any }) - return _constructor(function(spawnEvt: Signal<()>, retEvt: Signal, Status>) - spawnEvt:Fire() + return _constructor( + function( + spawnEvt: Signal<()>, + retEvt: Signal, Status> + ) + spawnEvt:Fire() - local ok, ret = pcall(fn, table.unpack(args)) - local res: Result = if ok then Ok(ret) else Err(ret) - retEvt:Fire(res, "ready") - end) + local ok, ret = pcall(fn, table.unpack(args)) + local res: Result = if ok then Ok(ret) else Err(ret) + retEvt:Fire(res, "ready") + end + ) end --[=[ @@ -146,33 +153,33 @@ end @return (Status, Option) -- Returns the [Status] and an optional return if completed ]=] function Future.poll(self: Future): (Status, Option) - if self._status == "initialized" then - self._retEvt:Connect(function(firedRet, status: Status) - self._status = status - self._ret = firedRet + if self._status == "initialized" then + self._retEvt:Connect(function(firedRet, status: Status) + self._status = status + self._ret = firedRet - -- Cleanup - coroutine.yield(self._thread) - coroutine.close(self._thread) + -- Cleanup + coroutine.yield(self._thread) + coroutine.close(self._thread) - self._spawnEvt:DisconnectAll() - self._retEvt:DisconnectAll() - end) + self._spawnEvt:DisconnectAll() + self._retEvt:DisconnectAll() + end) - self._spawnEvt:Connect(function() - self._status = "pending" - end) + self._spawnEvt:Connect(function() + self._status = "pending" + end) - coroutine.resume(self._thread, self._spawnEvt, self._retEvt) - end + coroutine.resume(self._thread, self._spawnEvt, self._retEvt) + end - if self._status == "pending" then - -- Just wait a bit more for the signal to fire - task.wait(0.01) - end + if self._status == "pending" then + -- Just wait a bit more for the signal to fire + task.wait(0.01) + end - local retOpt = if self._ret == nil then None() else Some(self._ret) - return self._status, retOpt + local retOpt = if self._ret == nil then None() else Some(self._ret) + return self._status, retOpt end --[=[ @@ -183,8 +190,8 @@ end @param self Future ]=] function Future.cancel(self: Future) - self._retEvt:Fire(nil :: any, "cancelled") - self._status = "cancelled" + self._retEvt:Fire(nil :: any, "cancelled") + self._status = "cancelled" end --[=[ @@ -197,14 +204,14 @@ end @return T -- The value returned by the function on completion ]=] function Future.await(self: Future): T - while true do - local status: Status, ret: Option = self:poll() + while true do + local status: Status, ret: Option = self:poll() - if status == "ready" then - -- Safe to unwrap, we know it must not be nil - return ret:unwrap() - end - end + if status == "ready" then + -- Safe to unwrap, we know it must not be nil + return ret:unwrap() + end + end end return Future diff --git a/lib/option.luau b/lib/option.luau index 4a20824..e8a75c1 100644 --- a/lib/option.luau +++ b/lib/option.luau @@ -27,8 +27,8 @@ local tableEq = require("util").tableEq local Option = {} export type Option = typeof(Option) & { - _optValue: T?, - typeId: "Option", + _optValue: T?, + typeId: "Option", } --[=[ @@ -37,7 +37,7 @@ export type Option = typeof(Option) & { No value. ]=] function None(): Option - return Option.new(nil) :: Option + return Option.new(nil) :: Option end --[=[ @@ -46,7 +46,7 @@ end Some value of type `T`. ]=] function Some(val: T): Option - return Option.new(val) :: Option + return Option.new(val) :: Option end --[=[ @@ -58,46 +58,49 @@ end @return Option ]=] function Option.new(val: T?) - return setmetatable( - { - _optValue = val, - typeId = "Option", - } :: Option, - { - __index = Option, - __tostring = function(self: Option) - if self._optValue == nil then - return `{self.typeId}::None` - else - return `{self.typeId}::Some({self._optValue})` - end - end, + return setmetatable( + { + _optValue = val, + typeId = "Option", + } :: Option, + { + __index = Option, + __tostring = function(self: Option) + if self._optValue == nil then + return `{self.typeId}::None` + else + return `{self.typeId}::Some({self._optValue})` + end + end, - __eq = function(self: Option, other: Option): boolean - if typeof(self._optValue) == "table" and typeof(other._optValue) == "table" then - return tableEq(self._optValue, other._optValue) - else - return self._optValue == other._optValue - end - end, - __lt = function(self: Option, other: Option): boolean - if self:isSome() and other:isSome() then - return (self._optValue :: any) < (other._optValue :: any) - end + __eq = function(self: Option, other: Option): boolean + if + typeof(self._optValue) == "table" + and typeof(other._optValue) == "table" + then + return tableEq(self._optValue, other._optValue) + else + return self._optValue == other._optValue + end + end, + __lt = function(self: Option, other: Option): boolean + if self:isSome() and other:isSome() then + return (self._optValue :: any) < (other._optValue :: any) + end - return false - end, - __le = function(self: Option, other: Option): boolean - if self:isSome() and other:isSome() then - return (self._optValue :: any) <= (other._optValue :: any) - end + return false + end, + __le = function(self: Option, other: Option): boolean + if self:isSome() and other:isSome() then + return (self._optValue :: any) <= (other._optValue :: any) + end - return false - end, - } + return false + end, + } - -- TODO: Implement __iter, once iterators traits exist - ) + -- TODO: Implement __iter, once iterators traits exist + ) end --[=[ @@ -109,7 +112,7 @@ end @return boolean ]=] function Option.isSome(self: Option): boolean - return self._optValue ~= nil + return self._optValue ~= nil end --[=[ @@ -123,8 +126,8 @@ end @return boolean ]=] function Option.isSomeAnd(self: Option, op: (val: T) -> boolean): boolean - -- We know that it's not None, so this is fine - return self:isSome() and op(self._optValue :: T) + -- We know that it's not None, so this is fine + return self:isSome() and op(self._optValue :: T) end --[=[ @@ -136,7 +139,7 @@ end @return boolean ]=] function Option.isNone(self: Option): boolean - return not self:isSome() + return not self:isSome() end --[=[ @@ -161,11 +164,13 @@ end @return boolean ]=] function Option.expect(self: Option, msg: string): T | never - if self:isSome() then - return self._optValue :: T - end + if self:isSome() then + return self._optValue :: T + end - return error(`panic: {msg}; expected Option to be type Option::Some(T), got Option::None`) + return error( + `panic: {msg}; expected Option to be type Option::Some(T), got Option::None` + ) end --[=[ @@ -181,11 +186,11 @@ end @return T | never ]=] function Option.unwrap(self: Option): T | never - if self:isSome() then - return self._optValue :: T - end + if self:isSome() then + return self._optValue :: T + end - return error("called `Option:unwrap()` on a `None` value") + return error("called `Option:unwrap()` on a `None` value") end --[=[ @@ -207,11 +212,11 @@ end @return T ]=] function Option.unwrapOr(self: Option, default: T): T - if self:isSome() then - return self._optValue :: T - end + if self:isSome() then + return self._optValue :: T + end - return default + return default end --[=[ @@ -234,11 +239,11 @@ end @return T ]=] function Option.unwrapOrElse(self: Option, default: () -> T): T - if self:isSome() then - return self._optValue :: T - end + if self:isSome() then + return self._optValue :: T + end - return default() + return default() end --[=[ @@ -266,17 +271,17 @@ end @return Option ]=] function Option.map(self: Option, op: (x: T) -> U?): Option - if self:isSome() then - local val = op(self._optValue :: T) + if self:isSome() then + local val = op(self._optValue :: T) - if val == nil then - return None() - end + if val == nil then + return None() + end - return Some(val) - end + return Some(val) + end - return None() + return None() end --[=[ @@ -303,11 +308,11 @@ end @return Option ]=] function Option.inspect(self: Option, op: (x: T) -> ()): Option - if self:isSome() then - op(self._optValue :: T) - end + if self:isSome() then + op(self._optValue :: T) + end - return self + return self end --[=[ @@ -333,11 +338,11 @@ end @return Option ]=] function Option.mapOr(self: Option, default: U, op: (val: T) -> U) - if self:isSome() then - return op(self._optValue :: T) - end + if self:isSome() then + return op(self._optValue :: T) + end - return default + return default end --[=[ @@ -371,17 +376,21 @@ end @param op (val: T) -> U -- The function to apply @return Option ]=] -function Option.mapOrElse(self: Option, default: () -> U, op: (val: T) -> U): U - if self:isSome() then - return op(self._optValue :: T) - end +function Option.mapOrElse( + self: Option, + default: () -> U, + op: (val: T) -> U +): U + if self:isSome() then + return op(self._optValue :: T) + end - return default() + return default() end -- TODO: Iterator traits function Option.iter(): never - return error("Unimplemented: `Option:iter()`") + return error("Unimplemented: `Option:iter()`") end --[=[ @@ -415,11 +424,11 @@ end @return Option ]=] function Option.and_(self: Option, optb: Option): Option - if self:isSome() then - return optb - end + if self:isSome() then + return optb + end - return None() + return None() end --[=[ @@ -462,11 +471,11 @@ end @return Option ]=] function Option.andThen(self: Option, op: (val: T) -> Option): Option - if self:isSome() then - return op(self._optValue :: T) - end + if self:isSome() then + return op(self._optValue :: T) + end - return None() + return None() end --[=[ @@ -492,14 +501,17 @@ end @param predicate (val: T) -> boolean -- The predicate function which must match an element @return Option ]=] -function Option.filter(self: Option, predicate: (val: T) -> boolean): Option - if self:isSome() then - if predicate(self._optValue :: T) then - return self - end - end +function Option.filter( + self: Option, + predicate: (val: T) -> boolean +): Option + if self:isSome() then + if predicate(self._optValue :: T) then + return self + end + end - return None() + return None() end --[=[ @@ -534,11 +546,11 @@ end @return Option ]=] function Option.or_(self: Option, optb: Option): Option - if self:isSome() then - return self - end + if self:isSome() then + return self + end - return optb + return optb end --[=[ @@ -564,11 +576,11 @@ end @return Option ]=] function Option.orElse(self: Option, op: () -> Option): Option - if self:isSome() then - return self - end + if self:isSome() then + return self + end - return op() + return op() end --[=[ @@ -600,17 +612,17 @@ end @return Option ]=] function Option.xor(self: Option, optb: Option): Option - if self:isSome() and optb:isSome() then - return None() - end + if self:isSome() and optb:isSome() then + return None() + end - if self:isSome() then - return self - elseif optb:isSome() then - return optb - end + if self:isSome() then + return self + elseif optb:isSome() then + return optb + end - return None() + return None() end --[=[ @@ -638,8 +650,8 @@ end @return T ]=] function Option.insert(self: Option, val: T): T - self._optValue = val - return self._optValue :: T + self._optValue = val + return self._optValue :: T end --[=[ @@ -667,11 +679,11 @@ end @return T ]=] function Option.getOrInsert(self: Option, val: T): T - if self:isNone() then - self._optValue = val - end + if self:isNone() then + self._optValue = val + end - return self._optValue :: T + return self._optValue :: T end --[=[ @@ -695,13 +707,13 @@ end @return Option ]=] function Option.take(self: Option): Option - if self:isSome() then - local val = self._optValue :: T - self._optValue = nil - return Some(val) - end + if self:isSome() then + local val = self._optValue :: T + self._optValue = nil + return Some(val) + end - return None() + return None() end --[=[ @@ -728,14 +740,14 @@ end @return Option ]=] function Option.replace(self: Option, val: T): Option - local current: Option = self - self._optValue = val + local current: Option = self + self._optValue = val - if current:isNone() then - return current - end + if current:isNone() then + return current + end - return Some(current._optValue :: T) + return Some(current._optValue :: T) end --[=[ @@ -757,11 +769,11 @@ end @return boolean ]=] function Option.contains(self: Option, val: T): boolean - if self:isSome() then - return self._optValue == val - end + if self:isSome() then + return self._optValue == val + end - return false + return false end --[=[ @@ -786,11 +798,11 @@ end @return Option<{T | U}> ]=] function Option.zip(self: Option, other: Option): Option<{ T | U }> - if self:isSome() and other:isSome() then - return Some({ self._optValue, other._optValue }) - end + if self:isSome() and other:isSome() then + return Some { self._optValue, other._optValue } + end - return None() + return None() end --[=[ @@ -829,16 +841,20 @@ end @param op (x: T, y: U) -> R? @return Option ]=] -function Option.zipWith(self: Option, other: Option, op: (x: T, y: U) -> R?): Option - if self:isSome() and other:isSome() then - local computed = op(self._optValue :: T, other._optValue :: U) +function Option.zipWith( + self: Option, + other: Option, + op: (x: T, y: U) -> R? +): Option + if self:isSome() and other:isSome() then + local computed = op(self._optValue :: T, other._optValue :: U) - if computed ~= nil then - return Some(computed :: R) - end - end + if computed ~= nil then + return Some(computed :: R) + end + end - return None() + return None() end --[=[ @@ -861,13 +877,17 @@ end @return (Option, Option) ]=] function Option.unzip(self: Option): (Option, Option) - if self:isSome() then - if self:isSome() and typeof(self._optValue) == "table" and #self._optValue == 2 then - return Some(self._optValue[1] :: A), Some(self._optValue[2] :: B) - end - end + if self:isSome() then + if + self:isSome() + and typeof(self._optValue) == "table" + and #self._optValue == 2 + then + return Some(self._optValue[1] :: A), Some(self._optValue[2] :: B) + end + end - return None(), None() + return None(), None() end --[=[ @@ -887,7 +907,7 @@ end @return T? ]=] function Option.unwrapUnchecked(self: Option): T? - return self._optValue + return self._optValue end --[=[ @@ -908,11 +928,11 @@ end @return string ]=] function Option.display(self: Option): string - return tostring(self) + return tostring(self) end return { - Option = Option, - Some = Some, - None = None, + Option = Option, + Some = Some, + None = None, } diff --git a/lib/result.luau b/lib/result.luau index deca2dc..ee35570 100644 --- a/lib/result.luau +++ b/lib/result.luau @@ -66,9 +66,9 @@ local tableEq = require("util").tableEq local Result = {} export type Result = typeof(Result) & { - _value: T, - _error: E, - typeId: "Result", + _value: T, + _error: E, + typeId: "Result", } --[=[ @@ -80,7 +80,7 @@ export type Result = typeof(Result) & { @return Result ]=] function Ok(val: T): Result - return Result.new(val, (nil :: unknown) :: E) + return Result.new(val, (nil :: unknown) :: E) end --[=[ @@ -92,7 +92,7 @@ end @return Result ]=] function Err(err: E): Result - return Result.new((nil :: unknown) :: T, err) + return Result.new((nil :: unknown) :: T, err) end --[=[ @@ -107,42 +107,44 @@ end @return Result ]=] function Result.new(val: T, err: E) - return setmetatable( - { - _value = val, - _error = err, - typeId = "Result", - } :: Result, - { - __index = Result, - __tostring = function(self: Result) - if self:isOk() then - return `{self.typeId}::Ok({self._value})` - end + return setmetatable( + { + _value = val, + _error = err, + typeId = "Result", + } :: Result, + { + __index = Result, + __tostring = function(self: Result) + if self:isOk() then + return `{self.typeId}::Ok({self._value})` + end - if self:isErr() then - return `{self.typeId}::Err({self._error})` - end + if self:isErr() then + return `{self.typeId}::Err({self._error})` + end - return `{self.typeId}` - end, - __eq = function(self: Result, other: Result) - if - typeof(self._value) ~= "table" - and typeof(self._error) ~= "table" - and typeof(other._value) ~= "table" - and typeof(other._error) ~= "table" - then - return self._value == other._value and self._error == other._error - end + return `{self.typeId}` + end, + __eq = function(self: Result, other: Result) + if + typeof(self._value) ~= "table" + and typeof(self._error) ~= "table" + and typeof(other._value) ~= "table" + and typeof(other._error) ~= "table" + then + return self._value == other._value + and self._error == other._error + end - return tableEq(self._value, other._value) and tableEq(self._error, other._error) - end, + return tableEq(self._value, other._value) + and tableEq(self._error, other._error) + end, - -- TODO: Implement equality and arithmetic metamethods - -- TODO: Implement __iter, once iterators traits exist - } - ) + -- TODO: Implement equality and arithmetic metamethods + -- TODO: Implement __iter, once iterators traits exist + } + ) end --[=[ @@ -162,11 +164,11 @@ end @return boolean ]=] function Result.isOk(self: Result): boolean - if self._value == nil then - return false - end + if self._value == nil then + return false + end - return true + return true end --[=[ @@ -190,8 +192,11 @@ end @param predicate (val: T?) -> boolean @return boolean ]=] -function Result.isOkAnd(self: Result, predicate: (val: T?) -> boolean): boolean - return self:isOk() and predicate(self._value) +function Result.isOkAnd( + self: Result, + predicate: (val: T?) -> boolean +): boolean + return self:isOk() and predicate(self._value) end --[=[ @@ -211,7 +216,7 @@ end @return boolean ]=] function Result.isErr(self: Result) - return not self:isOk() + return not self:isOk() end --[=[ @@ -235,12 +240,15 @@ end @param predicate (val: T?) -> boolean @return boolean ]=] -function Result.isErrAnd(self: Result, predicate: (val: E) -> boolean): boolean - if self:isErr() then - return predicate(self._error) - end +function Result.isErrAnd( + self: Result, + predicate: (val: E) -> boolean +): boolean + if self:isErr() then + return predicate(self._error) + end - return false + return false end --[=[ @@ -280,11 +288,11 @@ end @return Result ]=] function Result.map(self: Result, op: (val: T) -> U): Result - if self:isOk() then - return Result.new(op(self._value), self._error) :: Result - end + if self:isOk() then + return Result.new(op(self._value), self._error) :: Result + end - return Err(self._error) + return Err(self._error) end --[=[ @@ -311,11 +319,11 @@ end @return U ]=] function Result.mapOr(self: Result, default: U, op: (val: T) -> U): U - if self:isOk() then - return op(self._value) - end + if self:isOk() then + return op(self._value) + end - return default + return default end --[=[ @@ -339,12 +347,16 @@ end @param op (val: T) -> U @return U ]=] -function Result.mapOrElse(self: Result, default: (val: E) -> U, op: (val: T) -> U): U - if self:isOk() then - return op(self._value) - end +function Result.mapOrElse( + self: Result, + default: (val: E) -> U, + op: (val: T) -> U +): U + if self:isOk() then + return op(self._value) + end - return default(self._error) + return default(self._error) end --[=[ @@ -373,11 +385,11 @@ end @return Result ]=] function Result.mapErr(self: Result, op: (val: E) -> F): Result - if self:isErr() then - return Result.new(self._value, op(self._error)) - end + if self:isErr() then + return Result.new(self._value, op(self._error)) + end - return Ok(self._value) + return Ok(self._value) end --[=[ @@ -409,11 +421,11 @@ end @return Result ]=] function Result.inspect(self: Result, op: (val: T) -> ()): Result - if self:isOk() then - op(self._value) - end + if self:isOk() then + op(self._value) + end - return self + return self end --[=[ @@ -445,17 +457,17 @@ end @return Result ]=] function Result.inspectErr(self: Result, op: (val: E) -> ()): Result - if self:isErr() then - op(self._error) - end + if self:isErr() then + op(self._error) + end - return self + return self end -- TODO: Iterator traits -- selene: allow(unused_variable) function Result.iter(self: Result): never - return error("Unimplemented: `Result:iter()`") + return error("Unimplemented: `Result:iter()`") end --[=[ @@ -505,11 +517,11 @@ end @return T | never ]=] function Result.expect(self: Result, msg: string): T | never - if self:isOk() then - return self._value - end + if self:isOk() then + return self._value + end - return error(`panic: {msg}; {self._error}`) + return error(`panic: {msg}; {self._error}`) end --[=[ @@ -533,17 +545,19 @@ end @param self Result ]=] function Result.unwrap(self: Result): T | never - if self:isOk() then - return self._value - end + if self:isOk() then + return self._value + end - return error(`panic: \`Result:unwrap()\` called on an \`Err\` value: {self._error}`) + return error( + `panic: \`Result:unwrap()\` called on an \`Err\` value: {self._error}` + ) end -- TODO: default values for types -- selene: allow(unused_variable) function Result.unwrapOrDefault(self: Result): never - return error("Unimplemented: `Result:unwrapOrDefault()`") + return error("Unimplemented: `Result:unwrapOrDefault()`") end --[=[ @@ -563,11 +577,11 @@ end @return E | never ]=] function Result.expectErr(self: Result, msg: string): E | never - if self:isErr() then - return self._error - end + if self:isErr() then + return self._error + end - return error(`panic: {msg}; {self._error}`) + return error(`panic: {msg}; {self._error}`) end --[=[ @@ -590,21 +604,23 @@ end @return E | never ]=] function Result.unwrapErr(self: Result): E | never - if self:isErr() then - return self._error - end + if self:isErr() then + return self._error + end - return error(`panic: \`Result:unwrapErr()\` called on an \`Ok\` value: {self._value}`) + return error( + `panic: \`Result:unwrapErr()\` called on an \`Ok\` value: {self._value}` + ) end -- TODO: How the fuck do I implement this? -- selene: allow(unused_variable) function Result.intoOk(self: Result): never - return error("Unimplemented: `Result:intoOk()`") + return error("Unimplemented: `Result:intoOk()`") end -- selene: allow(unused_variable) function Result.intoErr(self: Result): never - return error("Unimplemented: `Result:intoErr()`") + return error("Unimplemented: `Result:intoErr()`") end --[=[ @@ -636,11 +652,11 @@ end @return Result ]=] function Result.and_(self: Result, res: Result): Result - if self:isOk() then - return res - end + if self:isOk() then + return res + end - return Err(self._error) + return Err(self._error) end --[=[ @@ -674,11 +690,11 @@ end @return Result ]=] function Result.andThen(self: Result, op: (...any) -> any): Result - if self:isOk() then - return op(self._value) - end + if self:isOk() then + return op(self._value) + end - return Err(self._error) :: Result + return Err(self._error) :: Result end --[=[ @@ -713,15 +729,15 @@ end @return Result | never ]=] function Result.or_(self: Result, res: Result): Result | never - if self:isErr() then - return res - end + if self:isErr() then + return res + end - if self:isOk() then - return Ok(self._value) :: Result - end + if self:isOk() then + return Ok(self._value) :: Result + end - return error("called `Result:or()` with an invalid value") + return error("called `Result:or()` with an invalid value") end --[=[ @@ -752,12 +768,15 @@ end @param op (x: E) -> Result @return Result ]=] -function Result.orElse(self: Result, op: (x: E) -> Result): Result - if self:isErr() then - return op(self._error) - end +function Result.orElse( + self: Result, + op: (x: E) -> Result +): Result + if self:isErr() then + return op(self._error) + end - return Ok(self._value) + return Ok(self._value) end --[=[ @@ -783,11 +802,11 @@ end @return T ]=] function Result.unwrapOr(self: Result, default: T): T - if self:isOk() then - return self._value - end + if self:isOk() then + return self._value + end - return default + return default end --[=[ @@ -809,11 +828,11 @@ end @return T ]=] function Result.unwrapOrElse(self: Result, op: (x: E) -> T): T - if self:isOk() then - return self._value - end + if self:isOk() then + return self._value + end - return op(self._error) + return op(self._error) end --[=[ @@ -837,11 +856,11 @@ end @return boolean ]=] function Result.contains(self: Result, val: T): boolean - if self:isOk() then - return self._value == val - end + if self:isOk() then + return self._value == val + end - return false + return false end --[=[ @@ -865,11 +884,11 @@ end @return boolean ]=] function Result.containsErr(self: Result, err: E): boolean - if self:isErr() then - return self._error == err - end + if self:isErr() then + return self._error == err + end - return false + return false end --[=[ @@ -890,11 +909,11 @@ end @return string ]=] function Result.display(self: Result): string - return tostring(self) + return tostring(self) end return { - Ok = Ok, - Err = Err, - Result = Result, + Ok = Ok, + Err = Err, + Result = Result, } diff --git a/lib/util.luau b/lib/util.luau index 743e413..fcb5bb2 100644 --- a/lib/util.luau +++ b/lib/util.luau @@ -1,38 +1,38 @@ -- From https://gist.github.com/sapphyrus/fd9aeb871e3ce966cc4b0b969f62f539 local function tableEq(tbl1, tbl2) - if tbl1 == tbl2 then - return true - elseif type(tbl1) == "table" and type(tbl2) == "table" then - for key1, value1 in pairs(tbl1) do - local value2 = tbl2[key1] + if tbl1 == tbl2 then + return true + elseif type(tbl1) == "table" and type(tbl2) == "table" then + for key1, value1 in pairs(tbl1) do + local value2 = tbl2[key1] - if value2 == nil then - -- avoid the type call for missing keys in tbl2 by directly comparing with nil - return false - elseif value1 ~= value2 then - if type(value1) == "table" and type(value2) == "table" then - if not tableEq(value1, value2) then - return false - end - else - return false - end - end - end + if value2 == nil then + -- avoid the type call for missing keys in tbl2 by directly comparing with nil + return false + elseif value1 ~= value2 then + if type(value1) == "table" and type(value2) == "table" then + if not tableEq(value1, value2) then + return false + end + else + return false + end + end + end - -- check for missing keys in tbl1 - for key2, _ in pairs(tbl2) do - if tbl1[key2] == nil then - return false - end - end + -- check for missing keys in tbl1 + for key2, _ in pairs(tbl2) do + if tbl1[key2] == nil then + return false + end + end - return true - end + return true + end - return false + return false end return { - tableEq = tableEq, + tableEq = tableEq, } diff --git a/mod.luau b/mod.luau index f5b03c6..9f1ff9c 100644 --- a/mod.luau +++ b/mod.luau @@ -1,14 +1,16 @@ -local luau = require("@lune/luau") local fs = require("@lune/fs") +local luau = require("@lune/luau") -local SIGNAL_PATH = "Packages/_Index/ffrostflame_luausignal@0.2.4/luausignal/src/init.luau" +local SIGNAL_PATH = + "Packages/_Index/ffrostflame_luausignal@0.2.4/luausignal/src/init.luau" local _signal = require(SIGNAL_PATH) export type Signal = _signal.luauSignal local signal: { - new: () -> Signal, -} = - luau.load('local task = require("@lune/task")\n' .. fs.readFile(SIGNAL_PATH))() + new: () -> Signal, +} = luau.load( + 'local task = require("@lune/task")\n' .. fs.readFile(SIGNAL_PATH) +)() return { - signal = signal, + signal = signal, }