Files
brolang/examples/programs/conditional_unwrap/main.bro
T

39 lines
841 B
Plaintext

# 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
}