mirror of
https://github.com/CompeyDev/rusty-luau.git
synced 2024-12-12 12:50:40 +00:00
41 lines
743 B
Text
41 lines
743 B
Text
local Option = {}
|
|
export type Option<T> = typeof(Option) & {
|
|
_optValue: T?,
|
|
typeId: "Option",
|
|
}
|
|
|
|
function None<T>(): Option<T>
|
|
return Option.new(nil) :: Option<T>
|
|
end
|
|
|
|
function Some<T>(val: T): 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<T>(self: Option<T>)
|
|
-- TODO: Return formatted enum variants too
|
|
|
|
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,
|
|
}
|