2019-07-06 15:52:50 +03:00
|
|
|
fn opt_err() ?string {return error('hi')}
|
|
|
|
|
|
|
|
fn test_err(){
|
|
|
|
v := opt_err() or {
|
|
|
|
assert err == 'hi'
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert false
|
|
|
|
println(v) // suppress not used error
|
|
|
|
}
|
2019-07-15 17:15:34 +03:00
|
|
|
|
|
|
|
fn err_call(ok bool) ?int {
|
|
|
|
if !ok {
|
|
|
|
return error('Not ok!')
|
|
|
|
}
|
|
|
|
return 42
|
|
|
|
}
|
|
|
|
|
2019-09-17 22:41:58 +03:00
|
|
|
fn ret_none() ?int {
|
|
|
|
//return error('wtf') //none
|
|
|
|
return none
|
2019-10-12 17:53:53 +03:00
|
|
|
}
|
2019-09-17 22:41:58 +03:00
|
|
|
|
2019-07-15 17:15:34 +03:00
|
|
|
fn test_option_for_base_type_without_variable() {
|
|
|
|
val := err_call(true) or {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
assert val == 42
|
2019-09-17 22:41:58 +03:00
|
|
|
println('hm')
|
|
|
|
val2 := ret_none() or {
|
|
|
|
println('yep')
|
|
|
|
return
|
2019-10-12 17:53:53 +03:00
|
|
|
}
|
|
|
|
println('$val2 should have been `none`')
|
|
|
|
assert false
|
|
|
|
// This is invalid:
|
|
|
|
// x := 5 or {
|
|
|
|
// return
|
|
|
|
// }
|
2019-07-15 17:15:34 +03:00
|
|
|
}
|
2019-10-01 06:33:03 +03:00
|
|
|
|
|
|
|
fn test_if_opt() {
|
|
|
|
if val := err_call(false) {
|
|
|
|
assert val == 42
|
2019-10-12 17:53:53 +03:00
|
|
|
}
|
2019-10-01 06:33:03 +03:00
|
|
|
assert 1 == 1
|
|
|
|
println('nice')
|
2019-10-12 17:53:53 +03:00
|
|
|
}
|