How to use functions with default arguments in Swift

Issue #696

Which methods do you think are used here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import Cocoa

struct Robot {
let a: Int
let b: Int
let c: Int

init(a: Int = 1, c: Int = 3) {
self.a = a
self.b = 0
self.c = c
print("Init with a=\(a) and c=\(c)")
}

init(a: Int = 1, b: Int = 2, c: Int = 3) {
self.a = a
self.b = b
self.c = c

print("Init with a\(a), b=\(b) and c=\(c)")
}
}

let r1 = Robot(c: 10)
let r2 = Robot(a: 5, c: 10)
let r3 = Robot(a: 5, b: 7, c: 10)
let r4 = Robot(a: 5)
let r5 = Robot(b: 5)

The log is

1
2
3
4
5
Init with a=1 and c=10
Init with a=5 and c=10
Init with a5, b=7 and c=10
Init with a=5 and c=3
Init with a1, b=5 and c=3

Comments