In Swift, how do backtick identifiers work, and what did Swift 6.2's raw identifiers add to them?
answer
- keywords as names
- backticks are not part of the name
- spaces and leading digits since 6.2
- always backticked at every use
basics
~20 sBackticks let a keyword such as default serve as a name. Since Swift 6.2 (SE-0451) they also form raw identifiers containing spaces, punctuation or a leading digit, such as descriptive test names, which must be backticked at every use.
solid answer
~40 sWrapping a keyword in backticks, as in `` let `default` = ... ``, makes it an ordinary identifier; the backticks are not part of the name, so `` `x` `` and `x` are the same. After a dot most keywords need no backticks at all, as in `item.for`. Swift 6.2 (SE-0451) extended the syntax to **raw identifiers**: names that may contain spaces, punctuation or a leading digit, such as `` func `checkout total includes 8% tax`() `` or an enum case `` `58mm` ``. Unlike escaped keywords, a raw identifier must be written with backticks everywhere, including after a dot. Some contents stay illegal: a backtick, a backslash, a line break, a name made only of operator characters (`` `+` ``) or only of whitespace.
code
swift · 11 linesenum ReceiptWidth { case `58mm`, `80mm` }
let `default` = ReceiptWidth.`80mm` // keyword used as a name: escaped identifier
func `checkout total includes 8% tax`() -> Bool { // raw identifier, Swift 6.2+
let subtotal = 100.0
return (subtotal * 1.08).rounded() == 108
}
print(`checkout total includes 8% tax`()) // true
print(`default` == .`80mm`) // truego deeper
Recall that backticks let a keyword such as default be used as a name, and that the backticks are not part of the name.
Explain where backticks can be omitted, after a dot for most keywords, and that Swift 6.2 added raw identifiers with spaces or a leading digit.
State the raw identifier rules, backticks everywhere and no operator-only or whitespace-only names, and the 6.2 toolchain requirement before adopting them in tests.
Decide where a team allows raw identifiers: descriptive test names are a clear win, while in regular API they force backticks on every caller.
## Identifiers and reserved words An **identifier** is a name for a constant, variable, function, type or case. An ordinary Swift identifier starts with a letter, an underscore or one of many Unicode characters, and continues with those plus digits. **Keywords** such as `class`, `default`, `let` and `for` are reserved by the grammar and cannot be used as plain names. ## Backticks for keywords: escaped identifiers Surrounding a keyword with backticks turns it into an ordinary name: ```swift let `default` = ReceiptWidth.`80mm` ``` Three rules complete the picture: - **The backticks are not part of the name.** `` `x` `` and `x` refer to the same identifier, so a backticked declaration of a non-keyword is just noise. - **After a dot, most keywords need no backticks.** A property declared as `` var `for`: String `` is used as `item.for`, because a member name cannot be confused with a statement keyword there. - **Use sparingly.** The Swift book's advice is to avoid keywords as names unless there is no other choice, as when mirroring an external API or a JSON key. ## Raw identifiers: Swift 6.2 SE-0451, implemented in Swift 6.2 and listed as a shipped language feature (`RawIdentifiers`) in the 6.4 compiler, extends the backtick syntax. Since 6.2, a backticked name may contain characters that ordinary identifiers cannot: - **spaces and punctuation**: `` func `checkout total includes 8% tax`() ``; - **a leading digit**: `` case `58mm`, `80mm` `` for receipt paper widths, or design tokens named `` `100` ``; - **names generated from external data**, such as resource names with dots. The motivating case is **test naming**. A test function's name exists only to describe the test, so a sentence in backticks can replace a camel-case name plus a separate display string; using it with a test framework is covered with that framework. The name a raw identifier declares is the real name of the declaration, not a label on top of it. The proposal's argument is that one descriptive name then serves everywhere the declaration is referenced, such as test reports, the debugger and crash logs. One visible consequence: printing an enum case declared as `` `58mm` `` with the Swift 6.4 toolchain shows the backticks in the output, because the case's name contains a character an ordinary identifier could not. ## Rules a raw identifier must follow | Rule | Example | Result | |---|---|---| | Backticks at every use, including after a dot | ``r.`line total` `` | required; `r.line total` does not parse | | Not only operator characters | ``func `+`(...)`` | parse error | | Not only whitespace | ``let ` ` = 1`` | parse error | | No backtick, backslash or line break inside | | not allowed | | Leading, trailing or inner spaces | `` `checkout total` `` | allowed | The operator rule keeps operators and identifiers distinct: `+` is declared as an operator function, never as a backticked name. The always-backticked rule exists because the parser could not otherwise tell where `line total` ends. Declarations exposed to Objective-C need an explicit Objective-C name, a detail covered with Objective-C bridging. ## When to use which 1. **Escaped keyword**: when an external name happens to be a keyword, such as a `default` field. Prefer renaming when you control the name. 2. **Raw identifier**: when the most natural name is a phrase or starts with a digit, and the code needs Swift 6.2 or later. Test names are the clearest win. 3. **Neither**: for everyday names. A raw identifier in regular API forces every caller to type backticks. ## Interview angle The common mistakes are believing the backticks become part of the name, thinking raw identifiers are a testing-only feature (they apply to any declaration), assuming they can be dropped after a dot, and dating them to an earlier release; Swift 5.0 added raw **strings** (`#"..."#`), a different feature with a similar name.
- Can a Swift raw identifier consist only of operator characters, such as a backticked plus sign?No. SE-0451 makes a backticked name made only of operator characters a parse error, so it is neither an identifier nor an operator. Declaring `` func `+`(a: Int, b: Int) -> Int `` fails; a custom `+` is declared as an operator function without backticks. A name made only of whitespace is rejected the same way.
- When does Swift require a semicolon, and can its multiline comments nest?A semicolon is optional at the end of a statement and required only between two statements on the same line, as in `let qty = 3; print(qty)`; without it the compiler reports `consecutive statements on a line must be separated by ';'`. Multiline comments do nest: an inner `/* ... */` inside an outer one closes only itself, which makes commenting out code that already holds comments safe.
- Saying the backticks become part of the identifier's name
- Claiming raw identifiers only work in test code
- Dropping a raw identifier's backticks after a dot
- Naming an operator function with a backticked plus sign
- Dating raw identifiers to Swift 5 because raw strings shipped then
Assumes Swift 6.4; raw identifiers (SE-0451) shipped in Swift 6.2, while backticks around keywords work in every Swift release.