A stroll through a Swift oddity: argument ordering

This little oddness popped up in IRC and I thought I’d share.

When creating methods and functions, you cannot normally re-order arguments. Try it (see this Swiftstub), and you’ll raise an error like this example’s “argument ‘a’ must precede argument ‘c’“.

But give each argument a default value, and you can re-order as desired (see this Swiftstub). Cool, right?

I’m guessing this is less an intentional feature than an artifact of Swift’s default value implementation. When defaults are available, you’d generally want to offer more permissive inclusion and exclusion.

As things stand, I think it’s kind of nifty. That said, I wouldn’t be surprised if it was either eliminated in the language or the expressibility expanded to all functions and methods with fully-articulated external names.

One Comment

  • You can reorder any arguments in a group of arguments with default values. It’s in the documentation. It does not mean that all arguments must have default value to be able to reorder.