Debian Codegraph @

tests/indent/swift.swift

raw
1 import Foundation
2
3 protocol Shape {
4 var area: Double { get }
5 func describe() -> String
6 }
7
8 class Rectangle: Shape {
9 var width: Double
10 var height: Double
11
12 var area: Double {
13 return width * height
14 }
15
16 var perimeter: Double {
17 get {
18 return 2 * (width + height)
19 }
20 set {
21 width = newValue / 4
22 }
23 }
24
25 var observed: Int = 0 {
26 willSet {
27 print(newValue)
28 }
29 didSet {
30 print(oldValue)
31 }
32 }
33
34 init(width: Double, height: Double) {
35 self.width = width
36 self.height = height
37 }
38
39 func describe() -> String {
40 return "rectangle"
41 }
42
43 func compute<T: Numeric>(values: [T]) -> [T] {
44 let doubled = values.map { value in
45 return value + value
46 }
47 return doubled
48 }
49 }
50
51 enum Direction {
52 case north
53 case south
54 case east
55 case west
56 }
57
58 func process(_ direction: Direction, count: Int) {
59 guard count > 0 else {
60 return
61 }
62
63 let numbers = [
64 1,
65 2,
66 3,
67 ]
68
69 let mapping = [
70 "a": 1,
71 "b": 2,
72 ]
73
74 for number in numbers {
75 if number > 1 {
76 print(number)
77 } else if number == 1 {
78 print("one")
79 } else {
80 print("zero")
81 }
82 }
83
84 var index = 0
85 while index < count {
86 index += 1
87 }
88
89 repeat {
90 index -= 1
91 } while index > 0
92
93 switch direction {
94 case .north:
95 let label = "up"
96 print(label)
97 case .south where count > 5:
98 print("down")
99 default:
100 print("side")
101 }
102
103 let result = compute(width: 10,
104 height: 20)
105 print(result)
106 }
107
108 let multi = """
109 unindented
110 indented
111 """