MVE:
/// Expect:
/// - output: "A::Foo(5)"
enum A {
Foo(i32)
Bar
// asserts that `.a` is `Foo` and adds one to its content.
fn foo_add_one_guard(mut this) {
guard (&mut this) is Foo(&mut x) else {
abort()
}
x += 1
}
// asserts that `.a` is `Foo` and adds one to its content.
fn foo_add_one_if(mut this) {
if (&mut this) is Foo(&mut x) {
x += 1
} else {
abort()
}
}
// asserts that `.a` is `Foo` and adds one to its content.
fn foo_add_one_match(mut this) {
match &mut this {
Foo(&mut x) => {
x += 1
}
else => {
abort()
}
}
}
}
fn main() {
mut a = A::Foo(4)
a.foo_add_one_guard()
println("{}", a)
}
The compiler errors in both foo_add_one_if
and foo_add_one_guard
saying that x
is not marked mutable:
[nix-shell:~/jakt]$ ./build/bin/jakt -cr mvs.jakt
Error: Assignment to immutable variable
βββββ¬β mvs.jakt:14:9
13 β
14 β x += 1
β ββββββ¬
β β°β Assignment to immutable variable
15 β }
βββββ΄β
Error: Assignment to immutable variable
βββββ¬β mvs.jakt:20:13
19 β if (&mut this) is Foo(&mut x) {
20 β x += 1
β ββββββ¬
β β°β Assignment to immutable variable
21 β
βββββ΄β
Ideally if x is B(<arg>)
would desugar to a match
instead of an if
, so that the semantics of pattern arguments have to be dealt with only once, which is already done correctly in match
.
Note that foo_add_one_match
would be a valid way to circunvent this issue, so it doesn't prevent the desired behavior from happening, just forces a path that uses match
.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too