Hi everyone,
I have a simple pitch for adding a comprehensive set of static properties for Locale.NumberingSystem
.
Feature name
- Proposal: SF-NNNN
- Authors: Gleb Fandeev
- Review Manager: TBD
- Status: Awaiting review
- Implementation: apple/swift-foundation#1055
Introduction
This proposal adds static properties to Locale.NumberingSystem
for all standard numbering systems defined in Unicode CLDR, making it easier to work with different numbering systems in Swift.
Motivation
Currently, to use a specific numbering system, developers need to create instances using string identifiers:
let arabic = Locale.NumberingSystem("arab")
This approach has several drawbacks:
- Lack of Discoverability: Developers may not be aware of all available numbering systems or their corresponding identifiers.
- Error-Prone: Manually typing string identifiers increases the risk of typos and mistakes.
- Reduced Readability: String literals provide less context compared to well-named constants.
- Inconsistency: Other Locale components like
Locale.LanguageCode
,Locale.Region
, andLocale.Script
already provide static properties for common identifiers, butLocale.NumberingSystem
does not.
By introducing predefined static properties for each numbering system, we can improve code safety, discoverability, readability, and maintain consistency across the Locale API.
Proposed solution
Extend Locale.NumberingSystem
to include static properties for each numbering system defined in the Unicode CLDR.
Example usage:
let numberingSystem = Locale.NumberingSystem.arabic
This allows developers to:
- Use autocomplete features to discover available numbering systems.
- Reduce typos and mistakes by avoiding manually typed strings:
let numberingSystem = Locale.NumberingSystem("arabic") // Incorrect identifier
- Improve code clarity with descriptive property names. For example,
Locale.NumberingSystem.simplifiedChinese
instead ofLocale.NumberingSystem("hans")
Detailed design
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.
@available(FoundationPreview 6.2, *)
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
extension Locale.NumberingSystem {
@_alwaysEmitIntoClient
public static var adlam: Locale.NumberingSystem { Locale.NumberingSystem("adlm") }
@_alwaysEmitIntoClient
public static var ahom: Locale.NumberingSystem { Locale.NumberingSystem("ahom") }
@_alwaysEmitIntoClient
public static var arabic: Locale.NumberingSystem { Locale.NumberingSystem("arab") }
@_alwaysEmitIntoClient
public static var arabicExtended: Locale.NumberingSystem { Locale.NumberingSystem("arabext") }
// ... all other numbering systems
}
The full list can be viewed in the implementation pull request. Variable names are assigned based on the descriptions provided in the Unicode CLDR.
Source compatibility
These changes are additive only and are not expected to have an impact on source compatibility.
Implications on adoption
This new API will have FoundationPreview 6.2 availability.
Acknowledgments
Thanks to @alobaili for highlighting this issue in their comment on the Swift forums, which inspired this proposal.