-
Notifications
You must be signed in to change notification settings - Fork 11
Add result to @batteries #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR description?
batteries/result.luau
Outdated
} | { | ||
success: false, | ||
traceback: string, | ||
err: string, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want this over a form where err
's type can be specified?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, kind of? The reason why is so that result failures can be appended. For example:
local function someOperation()
return result(false, "failed to do generic operation")
end
local function meow()
local call = someOperation()
if not call then
call.err = `failure in function "meow"; {call.err}`
end
end
local function meow2()
local call = someOperation()
if not call then
call.err = `failure in function "meow2"; {call.err}`
end
end
err as string is great for propagating messages. Maybe we could attach something like errMessage
and err
to have both? I can see the value in, say, an error tagged enum.
Co-authored-by: ariel <[email protected]>
Many operations involving the file system & networking can fail. One of the most common ways of handling this is pcall, but this is not type safe.
A common idiom is to have a Result type, somewhat similar to Rust's Result type. It's an extremely common and useful idiom. Any project written in Lute interacting with APIs which could error will end up encountering the problem of gracefully handling failure; the result battery significantly helps with that.