2019-10-25 22:03:42 +03:00
|
|
|
|
|
|
|
fn opt_err_with_code() ?string {return error_with_code('hi',137)}
|
|
|
|
fn test_err_with_code(){
|
|
|
|
v := opt_err_with_code() or {
|
|
|
|
assert err == 'hi'
|
|
|
|
assert errcode == 137
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert false
|
|
|
|
println(v) // suppress not used error
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2019-11-04 02:38:49 +03:00
|
|
|
|
|
|
|
fn for_opt_default() ?string {
|
|
|
|
return error('awww')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_opt_default() {
|
|
|
|
a := for_opt_default() or {
|
|
|
|
// panic(err)
|
|
|
|
'default'
|
|
|
|
}
|
|
|
|
assert a == 'default'
|
|
|
|
}
|
2019-11-13 21:47:05 +03:00
|
|
|
|
|
|
|
fn foo_ok() ?int {
|
|
|
|
return 777
|
2019-12-17 00:36:28 +03:00
|
|
|
}
|
2019-11-13 21:47:05 +03:00
|
|
|
|
2019-11-26 09:07:35 +03:00
|
|
|
fn foo_str() ?string {
|
|
|
|
return 'something'
|
|
|
|
}
|
|
|
|
|
2019-11-13 21:47:05 +03:00
|
|
|
fn test_q() {
|
|
|
|
//assert foo_ok()? == true
|
2019-12-17 00:36:28 +03:00
|
|
|
}
|
2019-11-26 09:07:35 +03:00
|
|
|
|
|
|
|
struct Person {
|
|
|
|
mut:
|
|
|
|
name string
|
|
|
|
age int
|
|
|
|
title ?string
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_field_or() {
|
|
|
|
name := foo_str() or {
|
|
|
|
'nada'
|
|
|
|
}
|
|
|
|
assert name == 'something'
|
|
|
|
|
|
|
|
mut p := Person {}
|
|
|
|
p.name = foo_str() or {
|
|
|
|
'nothing'
|
|
|
|
}
|
|
|
|
assert p.name == 'something'
|
|
|
|
|
|
|
|
p.age = foo_ok() or {
|
|
|
|
panic('no age')
|
|
|
|
}
|
|
|
|
assert p.age == 777
|
|
|
|
|
|
|
|
mytitle := p.title or {
|
|
|
|
'default'
|
|
|
|
}
|
|
|
|
assert mytitle == 'default'
|
2019-12-01 10:33:26 +03:00
|
|
|
}
|
2019-12-17 00:36:28 +03:00
|
|
|
|
|
|
|
struct Thing {
|
|
|
|
mut:
|
|
|
|
opt ?int
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_opt_field() {
|
|
|
|
mut t := Thing{}
|
|
|
|
t.opt = 5
|
|
|
|
val := t.opt or { return }
|
|
|
|
assert val == 5
|
|
|
|
}
|
2019-12-22 01:44:16 +03:00
|
|
|
|
|
|
|
fn opt_ptr(a &int) ?&int {
|
|
|
|
if isnil(a) {
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_opt_ptr() {
|
|
|
|
a := 3
|
|
|
|
r1 := opt_ptr(&a) or {
|
|
|
|
&int(0)
|
|
|
|
}
|
|
|
|
assert r1 == &a
|
|
|
|
r2 := opt_ptr(&int(0)) or {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
println('`$r2` should be none')
|
|
|
|
assert false
|
|
|
|
}
|
2020-02-02 04:53:23 +03:00
|
|
|
|
|
|
|
fn multi_return_opt(err bool) ?(string, string) {
|
|
|
|
if err {
|
|
|
|
return error('oops')
|
|
|
|
}
|
|
|
|
return 'hello', 'v'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_multi_return_opt() {
|
|
|
|
a, b := multi_return_opt(false) or {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
assert a == 'hello' && b == 'v'
|
|
|
|
_, _ := multi_return_opt(true) or {
|
|
|
|
assert err == 'oops'
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|