SE-0144: Allow Single Dollar Sign as a Valid Identifier
* Proposal: [SE-0144](0144-allow-single-dollar-sign-as-valid-identifier.md) * Author: [Ankur Patel](https://github.com/ankurp) * Review manager: [Chris Lattner](https://github.com/lattner) * Status: **Rejected** * Decision Notes: [Rationale](https://forums.swift.org/t/rejected-se-0144-allow-single-dollar-sign-as-a-valid-identifier/4340)
Introduction
The mainline Swift compiler emits an error message when the $ character (U+0024) is used as an identifier by itself, which is a source breaking change from Swift 3.0. For example:
let $ = 10
// OR
let $ : (Int) -> (Int) = { $0 * $0 }
// OR
class $ {}This proposal suggests reverting this change, enabling the use of $ as a valid identifier in future versions of Swift (>= 3.1).
Motivation
Some projects depend on the Dollar library, which uses $ as a namespace. The core team has decided to remove it as a valid character by merging this Pull Request
The reason behind the removal of $ character as a valid identifier is:
- it was never intended to be an identifier, and was never documented (e.g. by TSPL).
- the $ sigil is more operator/punctuation-like than identifier-like.
- the $ namespace is already used for anonymous closure arguments (when
followed by a number) and by debugger/REPL temporaries (when followed by a letter). Misuses of these cases were already properly rejected by the compiler, it is just the bare $ identifier that was accepted:
ERROR at line 1, col 5: expected numeric value following '$'
var $a = 5Proposed solution
Allow $ character (U+0024) to be used as a valid identifier without use of any tick marks ` $ `.
Impact on existing code
If this proposal is accepted, it will preserve the Swift 3.0 syntax which allows $ to be used as a valid identifier by itself, so there will be no impact on existing code (the TSPL will be updated to reflect this expansion of the grammar though).
If this proposal is rejected, then the $ identifier will be rejected in the Swift 4 compiler with a migration hint that changes uses to ` $ `. Users of the Dollar library will be affected.
Alternatives considered
The primarily alternative here is to allow for the breaking change and use ` $ ` as the identifier in the Dollar library, or for Dollar to adapt to another namespace.