Swift: Hidden gem or Obfuscation?
1 min readJul 28, 2016
func alphabet(a a: String = “a”, b: String = “b”) {
print(a, b)
}alphabet()
alphabet(b: “b”, a: “a”)
alphabet(a: “a”, b: “b”)
alphabet(a: “a”)
alphabet(b: “b”)
Regardless of the order of the default parameters, the above example still works as expected.
Hidden gem or Obfuscation?
UPDATE Aug 4, 2016:
As of Xcode 8 beta 3, the above phenomenon is no longer supported.
Function parameters with default arguments must now be specified in declaration order. A call site must always supply the arguments it provides to a function in their declared order:func requiredArguments(a: Int, b: Int, c: Int) {}
func defaultArguments(a: Int = 0, b: Int = 0, c: Int = 0) {}requiredArguments(a: 0, b: 1, c: 2)
requiredArguments(b: 0, a: 1, c: 2) // error defaultArguments(a: 0, b: 1, c: 2)
defaultArguments(b: 0, a: 1, c: 2) // error