8000 Refactor: Update the functions in @ref to handle error polymorphism by FlyCloudC · Pull Request #2257 · moonbitlang/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Refactor: Update the functions in @ref to handle error polymorphism #2257

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
Jun 13, 2025

Conversation

FlyCloudC
Copy link
Contributor

Note: Ref::pretect requires special modifications.

Copy link
peter-jerry-ye-code-review bot commented Jun 12, 2025
Redundant local variable introduction in protect method

Category
Correctness
Code Snippet
try f() catch { err => { ... } } else { r => { ... } }
Recommendation
Directly return the value in else branch without introducing r variable:
try f() catch { err => { self.val = old; raise err } } else { self.val = old; it }
Reasoning
The current implementation introduces an unnecessary variable 'r' in the else branch. In MoonBit, 'it' can be used to refer to the result value directly, making the code more concise without affecting functionality.

Test case could be more comprehensive

Category
Maintainability
Code Snippet
test "protect with error" { ... }
Recommendation
Add assertions to verify the intermediate state:
assert_eq(x.val, 2) // before error
assert_eq(r, 2) // after error
assert_eq(x.val, 1) // after restoration
Reasoning
While the test covers the basic error case, adding assertions about the intermediate state would make it clearer what exact behavior is being tested and help catch potential issues with state handling during errors.

Missing documentation for error conditions

Category
Maintainability
Code Snippet
pub fn[T, R] protect(self : Ref[T], a : T, f : () -> R raise?) -> R raise?
Recommendation
Add documentation explaining when errors can occur and how they are handled:
/// Temporarily sets the reference to value a, runs f, then restores the original value.
/// If f raises an error, the original value is restored before propagating the error.
Reasoning
With the addition of error handling, it's important to document the error behavior to help users understand when errors might occur and how the state is managed in such cases.

@coveralls
Copy link
Collaborator
coveralls commented Jun 12, 2025

Pull Request Test Coverage Report for Build 7268

Details

  • 3 of 3 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.001%) to 93.113%

Totals Coverage Status
Change from base Build 7267: 0.001%
Covered Lines: 8572
Relevant Lines: 9206

💛 - Coveralls

@peter-jerry-ye peter-jerry-ye merged commit ff6d24f into moonbitlang:main Jun 13, 2025
11 of 12 checks passed
@FlyCloudC FlyCloudC deleted the ref-err branch July 2, 2025 10:48
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.

3 participants
0