Swift: Hidden gem or Obfuscation?

Derrick Ho
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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response