[swift-evolution] Proposal: Remove the space before the colon in compiler-generated [Key : Value] dictionary types

Kevin Ballard kevin at sb.org
Thu Jan 14 13:35:45 CST 2016


Whenever dictionaries are referenced using their shorthand syntax, they end up printed as `[Key : Value]`, with the space before the colon. This can be seen whenever the compiler emits the type of a dictionary (e.g. printing the imported interface of an obj-c header, printing the type of a result in the REPL, or printing the type of a declaration in LLDB with `type lookup`), as well as in the documentation.

I think this looks weird. Dictionary type shorthand syntax is meant to look like a dictionary literal, and dictionary literals are almost universally written with no space before the colon. It makes sense to me for dictionary types to be written the same way, e.g. `[Key: Value]`. Not only does this look more like a dictionary literal, but I'd argue it also reduces confusion. Every other case that I can think of where you find space-colon-space syntax used is when declaring the bounds of a generic type parameter, or declaring the superclass/protocols of a type declaration (which is pretty similar to declaring the bounds). But in the syntax `[Key : Value]`, Value is not a bound on Key.

To that end, I'd like to update the compiler to remove the space before the colon, and update any doc comments that reference dictionary shorthand syntax (though I'm not sure if there actually are any). A radar would also have to be filed to cover checking to make sure the Xcode docsets are updated, though I'm hoping the Xcode docsets use the compiler to generate the interface declarations already.

Incidentally, it looks like the book already uses `[Key: Value]` syntax instead of `[Key : Value]` syntax. This means that whomever wrote the book believed `[Key: Value]` to be more sensible, and it also means that the first introduction most people have to Swift teaches them to use `[Key: Value]` instead of `[Key : Value]`.

-Kevin Ballard


More information about the swift-evolution mailing list