Today I Learned

hashrocket A Hashrocket project

The Order of Args With Default Values in Swift

Like many languages, the arguments to functions in Swift can have default values. As the documentation notes, it’s a best practice to place these arguments at the end of a function’s parameter list:

func foo(bar: String, baz: String = "omg", bah: String = "haha") {}
foo("<3 <3 <3") // `baz` and `bah` take on the values of "omg" and "haha" respectively

What I learned today is that when calling a function like this, the default arguments can be in any order:

foo("<3 <3 <3", bah: "first!", baz: ":(")


Joe Groff has a proposal on the mailing list to change this behavior.

Credit where credit’s due: I found out about this while reading the excellent Swift Weekly Brief.

See More #mobile TILs