2019-07-21 00:49:34 +02:00
|
|
|
fn foo() string {
|
2019-10-12 21:01:50 +02:00
|
|
|
println('foo()')
|
|
|
|
return 'foo'
|
|
|
|
}
|
2019-07-21 00:49:34 +02:00
|
|
|
|
2019-10-12 21:01:50 +02:00
|
|
|
fn foo2() string {
|
|
|
|
println('start')
|
|
|
|
defer { println('defer') }
|
|
|
|
defer { println('defer2') }
|
|
|
|
println('end')
|
|
|
|
return foo()
|
|
|
|
}
|
2019-07-21 00:49:34 +02:00
|
|
|
|
2019-10-12 21:01:50 +02:00
|
|
|
fn test_defer() {
|
|
|
|
assert foo2() == 'foo'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_num(i int, n mut Num) {
|
|
|
|
defer { n.val+=1 }
|
|
|
|
println("Hi")
|
|
|
|
if i < 5 {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
n.val+=1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_num_opt(n mut Num) ?int {
|
|
|
|
defer { n.val = 1 }
|
|
|
|
return 99
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Num {
|
|
|
|
mut:
|
|
|
|
val int
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_defer_early_exit() {
|
|
|
|
mut sum := Num{0}
|
|
|
|
for i in 0..10 {
|
|
|
|
set_num(i, mut sum)
|
|
|
|
}
|
|
|
|
println("sum: $sum.val")
|
|
|
|
assert sum.val == 15
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_defer_option() {
|
|
|
|
mut ok := Num{0}
|
|
|
|
set_num_opt(mut ok)
|
|
|
|
assert ok.val == 1
|
|
|
|
}
|