Skip to content

[Feature request] Better semantics for try/catch #216

@SkyyySi

Description

@SkyyySi

Note: this is a breaking change!

I would like to see the semantics of try/catch be changed to make it (in my opinion) more useful.

I think that

x = try
    do_something()
catch err
    handle_error(err)

should be changed from compiling to

local x = xpcall(function()
    return do_something()
end, function(err)
    return handle_error(err)
end)

into compiling to this:

local x = select(2, xpcall(function()
    return do_something()
end, function(err)
    return handle_error(err)
end))

The addition of select(2, [...]) causes the boolean that's normally returned by xpcall() to be skipped. This would feel more natural to use in many cases, and allow for patterns like this:

x = (try maybe_failing_operation()) ?? "default"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions