[1725989] | 1 | struct S { |
---|
| 2 | var i : Int, j : Int |
---|
| 3 | } |
---|
| 4 | var s = S( i : 3, j : 5 ) |
---|
| 5 | enum ADT { |
---|
| 6 | case I(Int) // int |
---|
| 7 | case F(Float) // float |
---|
| 8 | case S(S) // struct |
---|
| 9 | } |
---|
| 10 | var adt : ADT |
---|
| 11 | adt = ADT.I( 3 ); print( adt ) |
---|
| 12 | adt = .F( 3.5 ); print( adt ) |
---|
| 13 | adt = .S( s ); print( adt ) |
---|
| 14 | switch adt { |
---|
| 15 | case .I(let i): print( i ) |
---|
| 16 | case .F(let f): print( f ) |
---|
| 17 | case .S(let s): print( s.i, s.j ) |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | enum Week: Comparable { |
---|
| 21 | case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type |
---|
| 22 | func isWeekday() -> Bool { return self <= .Fri } // method |
---|
| 23 | func isWeekend() -> Bool { return .Sat <= self } // method |
---|
| 24 | }; |
---|
| 25 | var week : Week = Week.Mon; |
---|
| 26 | |
---|
| 27 | if week == .Mon { |
---|
| 28 | print( week ); |
---|
| 29 | } |
---|
| 30 | if week <= .Fri { |
---|
| 31 | print( "weekday" ); |
---|
| 32 | } |
---|
| 33 | if .Sat < week { |
---|
| 34 | print( "weekend" ); |
---|
| 35 | } |
---|
| 36 | switch week { // exhaustive |
---|
| 37 | case .Mon: print( "Mon" ) |
---|
| 38 | case .Tue: print( "Tue" ) |
---|
| 39 | case .Wed: print( "Wed" ) |
---|
| 40 | case .Thu: print( "Thu" ) |
---|
| 41 | case .Fri: print( "Fri" ) |
---|
| 42 | case .Sat: print( "Sat" ) |
---|
| 43 | case .Sun: print( "Sun" ) |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | enum WeekI: Comparable, CaseIterable { |
---|
| 47 | case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type |
---|
| 48 | }; |
---|
| 49 | var weeki : WeekI = WeekI.Mon; |
---|
| 50 | if weeki <= .Fri { |
---|
| 51 | print( "weekday" ); |
---|
| 52 | } |
---|
| 53 | for day in WeekI.allCases { |
---|
| 54 | print( day, terminator:" " ) |
---|
| 55 | } |
---|
| 56 | print( "" ) |
---|
| 57 | |
---|
| 58 | print( WeekI.allCases.count, WeekI.allCases ) |
---|
| 59 | |
---|
| 60 | enum WeekInt: Int, CaseIterable { |
---|
[7568e5c] | 61 | case Mon, Tue, Wed, Thu = 10, Fri = 14, |
---|
[1725989] | 62 | Sat = 4, Sun // auto-incrementing |
---|
| 63 | }; |
---|
| 64 | var weekInt : WeekInt = WeekInt.Mon; |
---|
| 65 | print( WeekInt.Thu.rawValue ); |
---|
| 66 | for day in WeekInt.allCases { |
---|
| 67 | print( day.rawValue, terminator:" " ) |
---|
| 68 | } |
---|
| 69 | print( "" ) |
---|
| 70 | |
---|
| 71 | enum WeekStr: String, CaseIterable { |
---|
| 72 | case Mon = "MON", Tue, Wed, Thu, Fri, |
---|
| 73 | Sat = "SAT", Sun |
---|
| 74 | }; |
---|
| 75 | for day in WeekStr.allCases { |
---|
| 76 | print( day.rawValue, terminator:" " ) |
---|
| 77 | } |
---|
| 78 | print( "" ) |
---|
| 79 | |
---|
| 80 | if let opt = WeekInt( rawValue: 0 ) { |
---|
| 81 | print( weekInt.rawValue, opt ) |
---|
| 82 | } else { |
---|
| 83 | print( "invalid weekday lookup" ) |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | // Local Variables: // |
---|
| 87 | // compile-command: "swift test.swift" // |
---|
| 88 | // End: // |
---|