Swift Raw Identifiers Note

Swift Raw Identifiers

Swift 6.2 introduces raw identifiers, enabling the use of characters previously forbidden in identifier names. This feature, implemented through SE-0451, allows identifiers to be enclosed in backticks. One key application is in Swift Testing, where raw identifiers can eliminate redundancy in test function naming. Instead of writing a descriptive string for the @Test attribute and a separate, conventional function name, a raw identifier can serve as both. This makes test function names descriptive and visible in the Test Navigator and reports. Another useful scenario is for enum cases that naturally correspond to numbers. Previously, prefixes were needed to avoid identifiers starting with digits. With raw identifiers, enum cases like 24 or 30 can be directly represented using backticks. This simplifies code by avoiding artificial prefixes for numerical enum cases. While the use of backticks may seem slightly cumbersome, it offers a significant improvement in code clarity and expressiveness.