|
| 1 | +# Feature name |
| 2 | + |
| 3 | +* Proposal: [SF-0012](0012-numbering-system-static-properties.md) |
| 4 | +* Authors: [Gleb Fandeev](https://github.com/glebfann) |
| 5 | +* Review Manager: TBD |
| 6 | +* Status: **Awaiting review** |
| 7 | +* Implementation: [apple/swift-foundation#1055](https://github.com/swiftlang/swift-foundation/pull/1055) |
| 8 | +* Review: ([pitch](https://forums.swift.org/t/pitch-add-static-properties-for-locale-numberingsystem/76203)) |
| 9 | + |
| 10 | +## Introduction |
| 11 | + |
| 12 | +This proposal adds static properties to `Locale.NumberingSystem` for all standard numbering systems defined in [Unicode CLDR](https://github.com/unicode-org/cldr/blob/latest/common/bcp47/number.xml), making it easier to work with different numbering systems in Swift. |
| 13 | + |
| 14 | +## Motivation |
| 15 | + |
| 16 | +Currently, to use a specific numbering system, developers need to create instances using string identifiers: |
| 17 | +```swift |
| 18 | +let arabic = Locale.NumberingSystem("arab") |
| 19 | +``` |
| 20 | +This approach has several drawbacks: |
| 21 | +- Lack of Discoverability: Developers may not be aware of all available numbering systems or their corresponding identifiers. |
| 22 | +- Error-Prone: Manually typing string identifiers increases the risk of typos and mistakes. |
| 23 | +- Reduced Readability: String literals provide less context compared to well-named constants. |
| 24 | +- Inconsistency: Other Locale components like `Locale.LanguageCode`, `Locale.Region`, and `Locale.Script` already provide static properties for common identifiers, but `Locale.NumberingSystem` does not. |
| 25 | + |
| 26 | +By introducing predefined static properties for each numbering system, we can improve code safety, discoverability, readability, and maintain consistency across the Locale API. |
| 27 | + |
| 28 | +## Proposed solution |
| 29 | + |
| 30 | +Extend `Locale.NumberingSystem` to include static properties for each numbering system defined in the Unicode CLDR. |
| 31 | + |
| 32 | +Example usage: |
| 33 | +```swift |
| 34 | +let numberingSystem = Locale.NumberingSystem.arabicIndic |
| 35 | +``` |
| 36 | +This allows developers to: |
| 37 | + |
| 38 | +- Use autocomplete features to discover available numbering systems. |
| 39 | +- Reduce typos and mistakes by avoiding manually typed strings: `let numberingSystem = Locale.NumberingSystem("arabic") // Incorrect identifier` |
| 40 | +- Improve code clarity with descriptive property names. For example, `Locale.NumberingSystem.simplifiedChinese` instead of `Locale.NumberingSystem("hans")` |
| 41 | + |
| 42 | +## Detailed design |
| 43 | + |
| 44 | +Add an extension to `Locale.NumberingSystem` containing static properties for each numbering system. The identifiers are sourced from the Unicode CLDR's numbering systems registry. |
| 45 | + |
| 46 | +```swift |
| 47 | +@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) |
| 48 | +extension Locale.NumberingSystem { |
| 49 | + /// Adlam digits. |
| 50 | + /// - Identifier: `"adlm"`. |
| 51 | + @_alwaysEmitIntoClient |
| 52 | + public static var adlam: Locale.NumberingSystem { Locale.NumberingSystem("adlm") } |
| 53 | + |
| 54 | + /// Ahom digits. |
| 55 | + /// - Identifier: `"ahom"`. |
| 56 | + @_alwaysEmitIntoClient |
| 57 | + public static var ahom: Locale.NumberingSystem { Locale.NumberingSystem("ahom") } |
| 58 | + |
| 59 | + /// Arabic-Indic digits. |
| 60 | + /// - Identifier: `"arab"`. |
| 61 | + @_alwaysEmitIntoClient |
| 62 | + public static var arabicIndic: Locale.NumberingSystem { Locale.NumberingSystem("arab") } |
| 63 | + |
| 64 | + /// Extended Arabic-Indic digits. |
| 65 | + /// - Identifier: `"arabext"`. |
| 66 | + @_alwaysEmitIntoClient |
| 67 | + public static var arabicIndicExtended: Locale.NumberingSystem { Locale.NumberingSystem("arabext") } |
| 68 | + |
| 69 | + // ... all other numbering systems |
| 70 | +} |
| 71 | +``` |
| 72 | +The full list can be viewed in the [implementation pull request](https://github.com/swiftlang/swift-foundation/pull/1055). Variable names are assigned based on the descriptions provided in the [Unicode CLDR](https://github.com/unicode-org/cldr/blob/latest/common/bcp47/number.xml). |
| 73 | + |
| 74 | +## Source compatibility |
| 75 | + |
| 76 | +These changes are additive only and are not expected to have an impact on source compatibility. |
| 77 | + |
| 78 | +## Implications on adoption |
| 79 | + |
| 80 | +This feature can be freely adopted and un-adopted in source code with no deployment constraints and without affecting source compatibility. |
| 81 | + |
| 82 | +## Acknowledgments |
| 83 | + |
| 84 | +Thanks to [Alobaili](https://forums.swift.org/u/alobaili/summary) for highlighting this issue in their [comment](https://forums.swift.org/t/fou-locale-components-language-and-language-components/54084/17) on the Swift forums, which inspired this proposal. |
0 commit comments