Greetings, traveler!
Swift 6.2 brings a subtle but powerful change to the language with SE-0451, allowing developers to use almost any characters in identifier names—by wrapping them in backticks (`). This makes your code more expressive, more readable, and in many cases, just more natural.
You can now write functions like this:
func `something special`() {
// ...
}
`something special`()
Numeric Enum Cases
Previously, modeling enums with numeric identifiers often required awkward naming hacks like case _500 or case core450. Imagine, you need to create a design system with specific font sizes, which were marked with integer identifiers by designer. With raw identifiers, you can write:
enum FontSize {
case `100`
case `150`
case `300`
case `450`
}
Tests
One of the most practical benefits of this feature is in Swift Testing. Instead of writing your test name in camel case and repeating a description string above, your test method can simply be the description:
import Testing
@Test("Some action")
func someAction() {
// test implementation
}
@Test
func `Some action`() {
// test implementation
}
One Small Gotcha: Operators Alone Aren’t Allowed
A minor rule worth noting: identifiers may include operator characters like + or -, but they cannot consist of only those characters. So `value+tax` is valid, but `+` is not.
If you enjoyed this article, please feel free to follow me on my social media:
It might be interesting:
- Simplifying Data Access in SwiftUI with @dynamicMemberLookup
- Three Practical Tools for Managing References and Detecting Memory Leaks in Swift
- Paging with Peek: Three Ways to Implement Paginated Scroll in SwiftUI
- Rendering HTML Text in SwiftUI with Custom Link Styling
- Conditional View Modifiers in SwiftUI: Convenience and Caveats