| 1 | type CEnum = int |
| 2 | |
| 3 | enum Enum { |
| 4 | value = 1 |
| 5 | } |
| 6 | |
| 7 | fn foo(n int) string { |
| 8 | return '${n}' |
| 9 | } |
| 10 | |
| 11 | fn bar(n CEnum) string { |
| 12 | return '${n}' |
| 13 | } |
| 14 | |
| 15 | fn test_cast_to_alias() { |
| 16 | e := Enum.value |
| 17 | mut ret_str := '' |
| 18 | |
| 19 | ret_str = foo(int(e)) |
| 20 | println(ret_str) |
| 21 | assert ret_str == '1' |
| 22 | |
| 23 | ret_str = bar(int(e)) |
| 24 | println(ret_str) |
| 25 | assert ret_str == '1' |
| 26 | |
| 27 | ret_str = foo(CEnum(e)) |
| 28 | println(ret_str) |
| 29 | assert ret_str == '1' |
| 30 | |
| 31 | ret_str = bar(CEnum(e)) |
| 32 | println(ret_str) |
| 33 | assert ret_str == '1' |
| 34 | } |
| 35 | |
| 36 | struct Foo { |
| 37 | x int |
| 38 | y string |
| 39 | } |
| 40 | |
| 41 | type Alias = Foo |
| 42 | |
| 43 | fn test_cast_to_alias_of_ref_struct() { |
| 44 | foo := &Foo(unsafe { nil }) |
| 45 | println(typeof(foo).name) |
| 46 | assert typeof(foo).name == '&Foo' |
| 47 | |
| 48 | bar := &Alias(unsafe { nil }) |
| 49 | println(typeof(bar).name) |
| 50 | assert typeof(bar).name == '&Alias' |
| 51 | } |
| 52 | |
| 53 | type Bar1 = Foo |
| 54 | type Bar2 = Foo |
| 55 | type Bar3 = Foo |
| 56 | |
| 57 | fn test_cast_to_from_alias() { |
| 58 | bar2 := Bar2{ |
| 59 | x: 100 |
| 60 | } |
| 61 | foo := Foo(bar2) |
| 62 | bar1 := Bar1(bar2) |
| 63 | bar3 := Bar3(bar2) |
| 64 | assert typeof(foo).name == 'Foo' |
| 65 | assert typeof(bar1).name == 'Bar1' |
| 66 | assert typeof(bar2).name == 'Bar2' |
| 67 | assert typeof(bar3).name == 'Bar3' |
| 68 | |
| 69 | foo1_1 := Foo(foo) |
| 70 | bar1_1 := Bar1(foo) |
| 71 | bar2_1 := Bar2(foo) |
| 72 | bar3_1 := Bar3(foo) |
| 73 | assert typeof(foo1_1).name == 'Foo' |
| 74 | assert typeof(bar1_1).name == 'Bar1' |
| 75 | assert typeof(bar2_1).name == 'Bar2' |
| 76 | assert typeof(bar3_1).name == 'Bar3' |
| 77 | } |
| 78 | |