mirror of
https://github.com/CompeyDev/rusty-luau.git
synced 2024-12-12 12:50:40 +00:00
feat: changes to formatting metamethods and examples
This commit is contained in:
parent
538fd90b0c
commit
f380daf7b2
3 changed files with 62 additions and 23 deletions
|
@ -1,7 +1,7 @@
|
|||
local Option = {}
|
||||
export type Option<T> = typeof(Option) & {
|
||||
_optValue: T?,
|
||||
typeId: "Option"
|
||||
typeId: "Option",
|
||||
}
|
||||
|
||||
function None<T>(): Option<T>
|
||||
|
@ -13,22 +13,29 @@ function Some<T>(val: T): Option<T>
|
|||
end
|
||||
|
||||
function Option.new<T>(val: T?)
|
||||
return setmetatable({
|
||||
return setmetatable(
|
||||
{
|
||||
_optValue = val,
|
||||
typeId = "Option"
|
||||
} :: Option<T>, {
|
||||
typeId = "Option",
|
||||
} :: Option<T>,
|
||||
{
|
||||
__index = Option,
|
||||
__tostring = function(self)
|
||||
-- Return formatted enum variants too
|
||||
__tostring = function<T>(self: Option<T>)
|
||||
-- TODO: Return formatted enum variants too
|
||||
|
||||
return `{self.typeId}<T>`
|
||||
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
|
||||
None = None,
|
||||
}
|
|
@ -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
20
test.luau
Normal 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()
|
Loading…
Reference in a new issue