feat: changes to formatting metamethods and examples

This commit is contained in:
Erica Marigold 2024-04-01 12:05:59 +05:30
parent 538fd90b0c
commit f380daf7b2
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
3 changed files with 62 additions and 23 deletions

View file

@ -1,34 +1,41 @@
local Option = {}
export type Option<T> = typeof(Option) & {
_optValue: T?,
typeId: "Option"
_optValue: T?,
typeId: "Option",
}
function None<T>(): Option<T>
return Option.new(nil) :: Option<T>
return Option.new(nil) :: Option<T>
end
function Some<T>(val: T): Option<T>
return Option.new(val) :: Option<T>
return Option.new(val) :: Option<T>
end
function Option.new<T>(val: T?)
return setmetatable({
_optValue = val,
typeId = "Option"
} :: Option<T>, {
__index = Option,
__tostring = function(self)
-- Return formatted enum variants too
return setmetatable(
{
_optValue = val,
typeId = "Option",
} :: Option<T>,
{
__index = Option,
__tostring = function<T>(self: Option<T>)
-- TODO: Return formatted enum variants too
return `{self.typeId}<T>`
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
-- TODO: Implement Option utility methods
Option = Option,
Some = Some,
None = None,
}

View file

@ -30,14 +30,14 @@ function Result.new<T, E>(val: T, err: E)
__index = Result,
__tostring = function<T, E>(self: Result<T, E>)
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}}<T, E>`
return `{self.typeId}<T, E>`
end,
__eq = function<T, E>(self: Result<T, E>, other: Result<T, E>)
if
@ -267,7 +267,7 @@ function Result.transpose<T, E>(self: Result<Option<T>, E>): Option<Result<T, E>
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<T, E>(self: Result<Option<T>, E>): Option<Result<T, E>
error("`Result` is not transposable")
end
local x: Result<Option<string>, 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

20
test.luau Normal file
View file

@ -0,0 +1,20 @@
local result = require("lib/result")
type Result<T, E> = result.Result<T, E>
local Ok = result.Ok
local Err = result.Err
local function canError(): Result<number, string>
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()