Skip to content

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

Merged
merged 3 commits into from
May 4, 2025
Merged

Conversation

ffrostfall
Copy link
Contributor

@ffrostfall ffrostfall commented Apr 20, 2025

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.

Copy link
Collaborator

@aatxe aatxe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description?

} | {
success: false,
traceback: string,
err: string,
Copy link
Collaborator

@aatxe aatxe Apr 21, 2025

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?

Copy link
Contributor Author

@ffrostfall ffrostfall Apr 22, 2025

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]>
@aatxe aatxe merged commit ad9e438 into luau-lang:primary May 4, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants