Proposed Key Path Syntax for Swift

David Smith, Michael LeHew, and Joe Groff, explaining why they chose backslash (\) as the syntax for their new key path proposal for Swift:

During review many different sigils were considered:

No Sigil: This matches function type references, but suffers from ambiguity with wanting to actually call a type property. Having to type let foo: KeyPath<Baz, Bar> while consistent with function type references, really is not that great (even for function type references).

Backtick: Borrowing from Lisp, backtick was what we used in initial discussions of this proposal (it was easy to write on a white-board), but it was not chosen because it is hard to type in Markdown, and comes dangerously close to conflicting with other parser intrinsics.

It kind of blows my mind that the ease of typing in Markdown would factor into a syntax decision for Swift. However, I disagree. It is not hard to type a literal backtick in Markdown. Here’s the relevant section of the Markdown syntax documentation.

In short, to include a literal backtick inside a <code> span, you can just use two backticks as the opening and closing delimiters. This input:

```Person.friends[0].name``

produces this HTML output:

<code>`Person.friends[0].name</code>

For the sake of clarity, you can include a space at the beginning (or end) of the delimited code span, which will be omitted from the output, like this:

`` `Person.friends[0].name``

Far be it for me to tell the Swift folks what to do, but I think backtick looks far better in the above example than backslash does. To me, backslash in any language should mean “escape the following character” and nothing else.

Tuesday, 18 April 2017