Skip to content

Add result to @batteries#159

Merged
aatxe merged 3 commits intoluau-lang:primaryfrom
ffrostfall:add-result-battery
May 4, 2025
Merged

Add result to @batteries#159
aatxe merged 3 commits intoluau-lang:primaryfrom
ffrostfall:add-result-battery

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
Member

@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
Member

@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 <aweiss@hey.com>
@aatxe aatxe merged commit ad9e438 into luau-lang:primary May 4, 2025
6 checks passed
green-real pushed a commit to green-real/lute that referenced this pull request May 19, 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.

---------

Co-authored-by: ariel <aweiss@hey.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants