summaryrefslogtreecommitdiff
path: root/AK/Try.h
AgeCommit message (Collapse)Author
2021-11-10AK+LibJS: Simplify MUST() and move it from LibJS to AK/Try.hAndreas Kling
This is generally useful so let's move it to AK. Also it seems that we don't need the temporary variable hack anymore, so let's lose that.
2021-11-08Kernel: Replace KResult and KResultOr<T> with Error and ErrorOr<T>Andreas Kling
We now use AK::Error and AK::ErrorOr<T> in both kernel and userspace! This was a slightly tedious refactoring that took a long time, so it's not unlikely that some bugs crept in. Nevertheless, it does pass basic functionality testing, and it's just real nice to finally see the same pattern in all contexts. :^)
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>