From f380daf7b2c4eb63206f4c9175f2afc062d295b5 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Mon, 1 Apr 2024 12:05:59 +0530 Subject: [PATCH] feat: changes to formatting metamethods and examples --- lib/option.luau | 45 ++++++++++++++++++++++++++------------------- lib/result.luau | 20 ++++++++++++++++---- test.luau | 20 ++++++++++++++++++++ 3 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 test.luau diff --git a/lib/option.luau b/lib/option.luau index 5d7fce6..13e4828 100644 --- a/lib/option.luau +++ b/lib/option.luau @@ -1,34 +1,41 @@ local Option = {} export type Option = typeof(Option) & { - _optValue: T?, - typeId: "Option" + _optValue: T?, + typeId: "Option", } function None(): Option - return Option.new(nil) :: Option + return Option.new(nil) :: Option end function Some(val: T): Option - return Option.new(val) :: Option + return Option.new(val) :: Option end function Option.new(val: T?) - return setmetatable({ - _optValue = val, - typeId = "Option" - } :: Option, { - __index = Option, - __tostring = function(self) - -- Return formatted enum variants too + return setmetatable( + { + _optValue = val, + typeId = "Option", + } :: Option, + { + __index = Option, + __tostring = function(self: Option) + -- TODO: Return formatted enum variants too - return `{self.typeId}` - end - }) + if self._optValue == nil then + return `{self.typeId}::None` + else + return `{self.typeId}::Some({self._optValue})` + end + end, + } + ) end return { - -- TODO: Implement Option utility methods - Option = Option, - Some = Some, - None = None -} \ No newline at end of file + -- TODO: Implement Option utility methods + Option = Option, + Some = Some, + None = None, +} diff --git a/lib/result.luau b/lib/result.luau index 8be4961..8a89be0 100644 --- a/lib/result.luau +++ b/lib/result.luau @@ -30,14 +30,14 @@ function Result.new(val: T, err: E) __index = Result, __tostring = function(self: Result) if self:isOk() then - return `{self.typeId}::Ok<{self._value}>` + return `{self.typeId}::Ok({self._value})` end if self:isErr() then - return `{self.typeId}::Err<{self._error}>` + return `{self.typeId}::Err({self._error})` end - return `{self.typeId}}` + return `{self.typeId}` end, __eq = function(self: Result, other: Result) if @@ -267,7 +267,7 @@ function Result.transpose(self: Result, E>): Option return None() elseif self:isOkAnd(function(val): boolean return val._optValue == nil - end) ~= nil then + end) then return Some(Ok(self._value._optValue)) elseif self:isErr() then return Some(Err(self._error)) @@ -275,3 +275,15 @@ function Result.transpose(self: Result, E>): Option error("`Result` is not transposable") end + +local x: Result, string> = Ok(None()) + +print(tostring(x)) + +return { + Ok = Ok, + Err = Err, + Result = Result, +} + +-- print(y:transpose()) -- this should have a typeerror, i need to fix this diff --git a/test.luau b/test.luau new file mode 100644 index 0000000..e4f900b --- /dev/null +++ b/test.luau @@ -0,0 +1,20 @@ +local result = require("lib/result") +type Result = result.Result +local Ok = result.Ok +local Err = result.Err + +local function canError(): Result + if math.round(math.random()) == 1 then + return Err("you DIED") + end + + return Ok(69) +end + +function main() + local val = canError():unwrap() + + print("got value: ", val) +end + +return main()