79 lines
1.9 KiB
Plaintext
79 lines
1.9 KiB
Plaintext
;; A kept if let with no else at the end of its chain gives an Option: Some of
|
|
;; the arm that ran, None when none did.
|
|
|
|
fn pick(a: Option(i32), k: i32) -> Option(i32)
|
|
if let Some(x) = a then x
|
|
elif k > 0 then k
|
|
|
|
fn only(a: Option(i32)) -> Option(i32)
|
|
if let Some(x) = a then x * 2
|
|
|
|
fn lead(k: i32, b: Option(i32)) -> Option(i32)
|
|
if k > 5
|
|
k
|
|
elif let Some(y) = b
|
|
y
|
|
|
|
;; An arm that returns stays Never, and the rest of the chain decides.
|
|
fn early(a: Option(i32)) -> Option(i32)
|
|
if let Some(x) = a
|
|
return None
|
|
elif true
|
|
3
|
|
|
|
;; An arm that is already an Option is the whole, flattened (decision 140).
|
|
fn flat(a: Option(i32), o: Option(i32)) -> Option(i32)
|
|
if let Some(x) = a then o
|
|
|
|
;; An Option of it wanted: the arm is Some of its value, and no match is the
|
|
;; outer None.
|
|
fn nest(a: Option(i32), o: Option(i32)) -> Option(Option(i32))
|
|
if let Some(x) = a then o
|
|
|
|
fn level(oo: Option(Option(i32)))
|
|
match oo
|
|
Some(o) -> if o? then println(o) else println("some none")
|
|
None -> println("none")
|
|
|
|
fn flat_chain(a: Option(i32), k: i32)
|
|
let r =
|
|
if let Some(x) = a
|
|
x
|
|
elif k > 0
|
|
None
|
|
show(r)
|
|
|
|
fn dyn_only(a: Option(i32)) -> dyn
|
|
if let Some(x) = a then x
|
|
|
|
fn show(o: Option(i32))
|
|
match o
|
|
Some(v) -> println(v)
|
|
None -> println("none")
|
|
|
|
fn main()
|
|
show(pick(Some(1), 0))
|
|
show(pick(None, 4))
|
|
show(pick(None, 0))
|
|
show(only(Some(3)))
|
|
show(only(None))
|
|
show(lead(9, None))
|
|
show(lead(1, Some(2)))
|
|
show(lead(1, None))
|
|
show(early(None))
|
|
show(early(Some(1)))
|
|
show(flat(Some(1), Some(4)))
|
|
show(flat(Some(1), None))
|
|
show(flat(None, Some(4)))
|
|
level(nest(Some(1), Some(4)))
|
|
level(nest(Some(1), None))
|
|
level(nest(None, Some(4)))
|
|
flat_chain(Some(3), 0)
|
|
flat_chain(None, 1)
|
|
flat_chain(None, 0)
|
|
println(dyn_only(Some(5)))
|
|
println(dyn_only(None))
|
|
;; As a statement it is unchanged.
|
|
if let Some(x) = Some(7)
|
|
println(x)
|