diff options
Diffstat (limited to 'v_windows/v/old/vlib/v/tests/nested_option_call_test.v')
-rw-r--r-- | v_windows/v/old/vlib/v/tests/nested_option_call_test.v | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/v_windows/v/old/vlib/v/tests/nested_option_call_test.v b/v_windows/v/old/vlib/v/tests/nested_option_call_test.v new file mode 100644 index 0000000..f2857f2 --- /dev/null +++ b/v_windows/v/old/vlib/v/tests/nested_option_call_test.v @@ -0,0 +1,67 @@ +fn ret(s string) string { + return s +} + +fn raise() ?string { + return none +} + +fn xx() { + s := ret(raise() or { return }) + println(s) +} + +fn test_nested_or() { + xx() +} + +fn xx_prop() ?string { + s := ret(raise() ?) + return s +} + +fn test_nested_propagation() { + a := xx_prop() or { 'propagated' } + assert a == 'propagated' +} + +struct St { +mut: + z f64 +} + +fn (mut s St) raise() ?f64 { + return error('some error') +} + +fn retf(f f64) f64 { + return f +} + +fn (mut s St) aa() { + f := retf(s.raise() or { return }) + s.z = 7.5 + println(f) +} + +fn test_nested_or_method_call() { + mut x := St{ + z: 2.25 + } + x.aa() + assert x.z == 2.25 +} + +fn (mut s St) aa_propagate() ? { + f := retf(s.raise() ?) + s.z = 7.5 + println(f) +} + +fn test_nested_propagation_method() { + mut x := St{ + z: 2.25 + } + x.aa_propagate() or { x.z = 13.0625 } + assert x.z == 13.0625 +} |