conditional optional unwrapping

This commit is contained in:
2026-06-21 20:38:12 +02:00
parent c90ada608e
commit f4194492cc
10 changed files with 391 additions and 12 deletions
@@ -0,0 +1,38 @@
# Milestone 5: conditional unwrapping `if opt |v| { ... }`.
# Tests a present optional binds and unwraps, a none takes the else branch, and
# that optional pointers (?*T) unwrap the same way.
main :: func() i32 {
total i32 = 0
# present optional scalar -> binds v to the unwrapped value
a ?i32 = 40
if a |v| {
total = total + v # 40
} else {
total = total + 99
}
# none -> else branch taken; the binding is not in scope there
b ?i32 = none
if b |v| {
total = total + v
} else {
total = total + 2 # 42
}
# optional pointer present -> binds q to a non-null @i32; deref proves it
n i32 = 0
p ?@i32 = &n
if p |q| {
total = total + q^ # +0
}
# optional pointer none -> skipped
z ?@i32 = none
if z |q| {
total = total + 1000
}
return total # expect exit code 42
}