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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions ref/ref.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn[T] new(x : T) -> Ref[T] {
/// assert_eq(@ref.new(1).map(fn(a){ a + 1 }).val, 2)
/// }
/// ```
pub fn[T, R] map(self : Ref[T], f : (T) -> R) -> Ref[R] {
pub fn[T, R] map(self : Ref[T], f : (T) -> R raise?) -> Ref[R] raise? {
{ val: f(self.val) }
}

Expand All @@ -65,12 +65,20 @@ pub fn[T, R] map(self : Ref[T], f : (T) -> R) -> Ref[R] {
/// assert_eq(x.val, 1)
/// }
/// ```
pub fn[T, R] protect(self : Ref[T], a : T, f : () -> R) -> R {
pub fn[T, R] protect(self : Ref[T], a : T, f : () -> R raise?) -> R raise? {
let old = self.val
self.val = a
let r = f()
self.val = old
r
try f() catch {
err => {
self.val = old
raise err
}
} else {
r => {
self.val = old
r
}
}
}

///|
Expand Down Expand Up @@ -106,7 +114,7 @@ test "swap" {
}

///|
pub fn[T] update(self : Ref[T], f : (T) -> T) -> Unit {
pub fn[T] update(self : Ref[T], f : (T) -> T raise?) -> Unit raise? {
self.val = f(self.val)
}

Expand Down
12 changes: 6 additions & 6 deletions ref/ref.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import(
)

// Values
fn[T, R] map(Ref[T], (T) -> R) -> Ref[R]
fn[T, R] map(Ref[T], (T) -> R raise?) -> Ref[R] raise?

fn[T] new(T) -> Ref[T]

fn[T, R] protect(Ref[T], T, () -> R) -> R
fn[T, R] protect(Ref[T], T, () -> R raise?) -> R raise?

fn[T] swap(Ref[T], Ref[T]) -> Unit

fn[T] update(Ref[T], (T) -> T) -> Unit
fn[T] update(Ref[T], (T) -> T raise?) -> Unit raise?

// Types and methods
fn[T, R] Ref::map(Self[T], (T) -> R) -> Self[R]
fn[T, R] Ref::map(Self[T], (T) -> R raise?) -> Self[R] raise?
fn[T] Ref::new(T) -> Self[T]
fn[T, R] Ref::protect(Self[T], T, () -> R) -> R
fn[T, R] Ref::protect(Self[T], T, () -> R raise?) -> R raise?
fn[T] Ref::swap(Self[T], Self[T]) -> Unit
fn[T] Ref::update(Self[T], (T) -> T) -> Unit
fn[T] Ref::update(Self[T], (T) -> T raise?) -> Unit raise?
impl[X : @quickcheck.Arbitrary] @quickcheck.Arbitrary for Ref[X]

// Type aliases
Expand Down
15 changes: 15 additions & 0 deletions ref/ref_test.mbt
7502
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ test "protect" {
assert_eq(x.val, 1)
}

///|
test "protect with error" {
let x = @ref.new(1)
assert_eq(x.val, 1)
let mut r = 0
let result = try? x.protect(2, fn() {
r = x.val
fail("")
r = -x.val
})
assert_true(result is Err(_))
assert_eq(r, 2)
assert_eq(x.val, 1)
}

///|
test "update with increment function" {
let a = @ref.new(1)
Expand Down
Loading
0