My bug report was closed "nothing to fix, intended behavior". It referenced this:
The language guide recommends:
Place parameters that don’t have default values at the beginning of a function’s parameter list, before the parameters that have default values.
but you can totally go against this recommendation if the func can be called with trailing closure syntax:
func f(_ a: Int = 1, _ b: Int = 2, zzz: () -> Void, xxx: () -> Void) {
print("You call f, a: \(a), b: \(b)")
zzz()
xxx()
}
print("\n1111")
// okay
f {
print("In closure 1111")
}
xxx: {
print("In closure 2222")
}
print("\n2222")
// okay a assigned with 111, b take default
f(111) {
print("In closure 1111")
}
xxx: {
print("In closure 2222")
}
print("\n3333")
// okay a and b overridden
f(111, 222) {
print("In closure 1111")
}
xxx: {
print("In closure 2222")
}
// you can even have non-closure params with no defaults follow params with defaults!!!
func g(_ a: Int = 1, _ b: Int = 2, c: Int, d: Int, zzz: () -> Void, xxx: () -> Void) {
print("You call g, a: \(a), b: \(b), c: \(c), d: \(d)")
zzz()
xxx()
}
print("\n4444")
// ok
g(c: 555, d: 666) {
print("In closure 1111")
}
xxx: {
print("In closure 2222")
}
print("\n5555")
// ok 111 assigned to a, b is default
g(111, c: 555, d: 666) {
print("In closure 1111")
}
xxx: {
print("In closure 2222")
}
print("\n6666")
// ok a and b overridden
g(111, 222, c: 555, d: 666) {
print("In closure 1111")
}
xxx: {
print("In closure 2222")
}
My guess is because trailing closure calling syntax is processed differently? Maybe parameters are match in reverse so it's okay to have params with defaults at the beginning?
It seems trailing closure calling syntax contradicts that recommendation: the ordering rule is reversed in fact.
Because of this, I think the compiler should reject any func with params with defaults precede params with no default unless this func can be called with trailing closure syntax.
And then if this func is not called with trailing closure syntax, the compile error should say params with defaults must be explicitly specified at call site, or change calling to trailing closure syntax. This would totally clear up my original confusion. Don't just show this:
Missing argument for parameter #1 in call