v2 / vlib / v / tests / options / option_selector_nested_unwrap_test.v
46 lines · 39 sloc · 530 bytes · 27f637a8dcb84709477cfe02f302e4cec57a40ab
Raw
1module main
2
3type Foo = string | int | f32
4
5struct Svc {
6mut:
7 log ?Foo
8}
9
10fn t(v string) !string {
11 return v
12}
13
14fn Svc.init(log ?Foo) Svc {
15 mut svc := Svc{
16 log: log
17 }
18 if svc.log != none {
19 if svc.log is string {
20 assert svc.log.str() == 'foo'
21 _ := t(svc.log) or { panic(err) }
22 } else {
23 assert false
24 }
25 assert true
26 }
27 return svc
28}
29
30struct CSvc {
31 Svc
32pub mut:
33 log ?Foo
34}
35
36pub fn CSvc.init(log ?Foo) CSvc {
37 mut c := CSvc{
38 log: log
39 }
40 c.Svc = Svc.init(log)
41 return c
42}
43
44fn test_main() {
45 CSvc.init('foo')
46}
47