From d5508bc6d04b705fa4902b634de1ea4e2d4d37ad Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Mon, 1 Apr 2024 22:35:55 +0530 Subject: [PATCH] feat: add equality metamethods --- lib/option.luau | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/option.luau b/lib/option.luau index 850ed96..c571444 100644 --- a/lib/option.luau +++ b/lib/option.luau @@ -21,17 +21,34 @@ function Option.new(val: T?) { __index = Option, __tostring = function(self: Option) - -- TODO: Return formatted enum variants too - 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 + return self._optValue == other._optValue + end, + __lt = function(self: Option, other: Option): boolean + if self:isSome() and other:isSome() then + -- FIXME: TypeError: Type T cannot be compared with < because it has no metatable + return (self._optValue :: T) < (other._optValue :: T) + end + + return false + end, + __le = function(self: Option, other: Option): boolean + if self:isSome() and other:isSome() then + -- FIXME: TypeError: Type T cannot be compared with <= because it has no metatable + return (self._optValue :: T) <= (other._optValue :: T) + end + + return false + end, } - -- TODO: Implement equality and arithmetic metamethods -- TODO: Implement __iter, once iterators traits exist ) end @@ -250,6 +267,10 @@ function Option.unzip(self: Option): (Option, Option) return None(), None() end +function Option.getInner(self: Option): T? + return self._optValue +end + return { Option = Option, Some = Some,