Swift `print` takes multiple arguments

I forget this too often, and write things like:

print("Size: \(thingy.getSize("Example"))")

Sometimes I feel like I’m really just writing stress-tests for the Swift syntax parser.

The above can actually be written a bit more simply, as:

print("Size:", thingy.getSize("Example"))

Not just fewer characters, but conceptually simpler – fewer nested parenthesis, and no nested string literals at all.

See, print‘s function signature is not in fact merely print(_ s: String), but:

func print<Target>(
    _ items: Any...,
    separator: String = " ",
    terminator: String = "\n",
    to output: inout Target
) where Target : TextOutputStream

(I’m ignoring the specialisation that omits the to parameter – conceptually there’s just one print function)

It’s a pity that Swift still doesn’t have splatting, as that’d enable a nicer way of printing collections, than having to use .joined(by: …).

Leave a Comment