summaryrefslogtreecommitdiff
path: root/AK/Try.h
AgeCommit message (Collapse)Author
2021-09-15AK: Rename the local variable in the TRY() macro to avoid name clashesLinus Groh
"result" is a tad bit too generic to provide a clash-free experience - we found instances in LibJS where this breaks already. Essentially this doesn't work: auto foo = TRY(bar(result)); Because it expands to the following within the TRY() scope: { auto result = bar(result); ... } And that of course fails: error: use of ‘result’ before deduction of ‘auto’ The simple solution here is to use a name that is much less likely to clash with anything used in the expression ("_temporary_result"). :^)
2021-09-05AK: Add a TRY(expression) macro to simplify the unwrap-or-return patternAndreas Kling
The way we use classes like Kernel::KResultOr<T> and AK::Result<T, E> makes checking for errors (and short-circuiting returns) quite verbose. This patch adds a new TRY(expression) macro that either evaluates to the released result of the expression if successful, or returns the error if not. Before: auto foo_or_error = get_foo(); if (foo_or_error.is_error()) return foo_or_error.release_error(); auto foo = foo_or_error.release_value(); After: auto foo = TRY(get_foo()); The macro uses a GNU C++ extension which is supported by GCC, Clang, Intel C++, and possibly others. It's not *ideal*, but since it makes our codebase considerably nicer, let's try(!) it out. :^) Co-authored-by: Ali Mohammad Pur <mpfard@serenityos.org>