|
| 1 | +# Allow more characters (like whitespaces and punctuations) for escaped identifiers |
| 2 | + |
| 3 | +* Proposal: [SE-XXXX] |
| 4 | + |
| 5 | +* Authors: [Alfredo Delli Bovi](https://github.com/adellibovi) |
| 6 | + |
| 7 | +* Review Manager: TBD |
| 8 | + |
| 9 | +* Status: **Awaiting review** |
| 10 | + |
| 11 | +* Implementation: [apple/swift#28966](https://github.com/apple/swift/pull/28966) |
| 12 | + |
| 13 | +## Introduction |
| 14 | +Swift has a beautiful concise yet expressive syntax. |
| 15 | +As part of that, escaped identifiers are adopted to allow usage of reserved keywords. |
| 16 | +This proposal wants to extend the character allowance for escaped identifiers with more Unicode scalars, like whitespace and punctuation. |
| 17 | +It will enable to have method names (or other identifiers) with a more readable and natural language like the following: |
| 18 | +```swift |
| 19 | +func `test validation should succeed when input is less then ten`() |
| 20 | +``` |
| 21 | +## Motivation |
| 22 | +Naming could be hard and having descriptive methods, like in tests, may result in declarations that are hard to read because of its lack of whitespace and punctuations or other symbols. Enabling natural language would improve readability. |
| 23 | + |
| 24 | +Maintainers of different projects under the [Swift Source Compatibility](https://swift.org/source-compatibility/#current-list-of-projects) uses, instead of Swift's method declaration, testing frameworks, like [Quick](https://github.com/Quick/Quick), because (among other reasons) how they can elegantly express tests descriptions. |
| 25 | + |
| 26 | +Other modern languages like [F#](https://fsharp.org) and [Kotlin](https://kotlinlang.org) saw the value in supporting natural language for escaped identifiers. Today, naming methods with spaces and punctuation are, for those languages, a standard for tests, widely adopted and supported by different test runners and reporting tools. |
| 27 | + |
| 28 | +## Proposed solution |
| 29 | +This proposal wants to extend the current grammar for every escaped identifier (properties, methods, types etc...) by allowing every Unicode scalar. |
| 30 | + |
| 31 | +A declaration to an escaped identifier will follow the existing back-ticked syntax. |
| 32 | +```swift |
| 33 | +func `test validation should succeed when input is less then ten`() |
| 34 | +var `some var` = 0 |
| 35 | +``` |
| 36 | + |
| 37 | +As per referencing. |
| 38 | +```swift |
| 39 | +`test validation should succeed when input is less then ten`() |
| 40 | +foo.`property with space` |
| 41 | +``` |
| 42 | +In fact, by allowing a larger set of characters, we will remove current limitations and, as an example, we will enable us to reference an operator, which currently produces an error. |
| 43 | +```swift |
| 44 | +let add = Int.`+` |
| 45 | +``` |
| 46 | + |
| 47 | +### Grammar |
| 48 | +This proposal wants to replace the following grammar: |
| 49 | +``` |
| 50 | +identifier → ` identifier-head identifier-characters opt ` |
| 51 | +``` |
| 52 | +with: |
| 53 | +``` |
| 54 | +identifier → ` escaped-identifier ` |
| 55 | +escaped-identifier -> Any Unicode scalar value except U+000A or U+000D or U+0060 |
| 56 | +``` |
| 57 | + |
| 58 | +### Objective-C Interoperability |
| 59 | +Objective-C declarations do not support every type of Unicode scalar value. |
| 60 | +If willing to expose an escaped identifier that includes a not supported Objective-C character, we can sanitize it using the existing `@objc` annotation like the following: |
| 61 | +```swift |
| 62 | +@objc(sanitizedName) |
| 63 | +``` |
| 64 | + |
| 65 | +## Source compatibility |
| 66 | +This feature is strictly additive. |
| 67 | + |
| 68 | +## Effect on ABI stability |
| 69 | +This feature does not affect the ABI. |
| 70 | + |
| 71 | +## Effect on API resilience |
| 72 | +This feature does not affect the API. |
| 73 | + |
| 74 | +## Alternatives considered |
| 75 | +It was considered to extend the grammars for methods declaration only, this was later discarded because we want to keep usage consistency and it would be hard to explain why an escaped identifier may support a certain set of characters in a context and a different one in another context. |
0 commit comments