| 1 | type SumType = f64 | int |
| 2 | |
| 3 | type MyByte = u8 |
| 4 | |
| 5 | struct Struct { |
| 6 | a int |
| 7 | } |
| 8 | |
| 9 | type Alias = int |
| 10 | |
| 11 | fn test_main() { |
| 12 | arr := [1, 2, 3] |
| 13 | mut t := ?[]int([]int{len: 10, init: 2}) |
| 14 | assert t != none |
| 15 | t = ?[]int(arr) |
| 16 | assert t != none |
| 17 | t = ?[]int([]int{len: 1, init: 0}) |
| 18 | assert t != none |
| 19 | |
| 20 | mut t2 := ?int(1) |
| 21 | assert t2 != none |
| 22 | mut t3 := ?f64(1.2) |
| 23 | assert t3 != none |
| 24 | mut t4 := ?string('') |
| 25 | assert t4 != none |
| 26 | mut t5 := ?SumType(1) |
| 27 | assert t5 != none |
| 28 | mut t6 := ?SumType(none) |
| 29 | assert t6 == none |
| 30 | mut t7 := ?Struct(Struct{}) |
| 31 | assert t7 != none |
| 32 | } |
| 33 | |
| 34 | fn test_cast() { |
| 35 | var := ?u8(1) |
| 36 | println(?u8(var)) |
| 37 | println(?u8(?u8(255))) |
| 38 | println(?int(none)) |
| 39 | println(?int(var?)) |
| 40 | |
| 41 | a := ?Struct{} |
| 42 | assert a == none |
| 43 | b := ?Struct(Struct{}) |
| 44 | assert b != none |
| 45 | |
| 46 | mut v1 := ?bool(1) |
| 47 | assert v1?.str() == 'true' |
| 48 | v1 = ?bool(true) |
| 49 | assert v1?.str() == 'true' |
| 50 | v1 = ?bool(0) |
| 51 | assert v1?.str() == 'false' |
| 52 | v1 = ?bool(false) |
| 53 | assert v1?.str() == 'false' |
| 54 | } |
| 55 | |
| 56 | fn test_cast_alias() { |
| 57 | assert '${?MyByte(0)}' == 'Option(MyByte(0))' |
| 58 | assert '${?MyByte(255)}' == 'Option(MyByte(255))' |
| 59 | assert '${?MyByte(?u8(0))}' == 'Option(MyByte(0))' |
| 60 | assert '${?MyByte(?u8(255))}' == 'Option(MyByte(255))' |
| 61 | } |
| 62 | |