-
Notifications
You must be signed in to change notification settings - Fork 3
Make Quantity extensible via RawRepresentable struct #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,70 @@ | ||
| /// A dimension of measurement. These may be combined to form composite dimensions and measurements | ||
| public enum Quantity: String, Sendable { | ||
| // TODO: Consider changing away from enum for extensibility | ||
| /// A dimension of measurement. These may be combined to form composite dimensions and measurements. | ||
| /// | ||
| /// `Quantity` is `RawRepresentable` rather than an enum so that callers can define their own | ||
| /// dimensions without modifying this package. Add one with a static extension: | ||
| /// | ||
| /// ```swift | ||
| /// extension Quantity { | ||
| /// static let money = Quantity(rawValue: "Money") | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// Equality and hashing are based on `rawValue`, so each distinct dimension needs a unique | ||
| /// raw string. Because the namespace is global across every module that links this package, | ||
| /// two modules that independently pick the same raw value are treated as the *same* dimension | ||
| /// and silently cancel against one another. Prefix custom raw values to avoid collisions — | ||
| /// e.g. `"Acme.Money"` rather than `"Money"`. | ||
| public struct Quantity: RawRepresentable, Hashable, Sendable { | ||
| public let rawValue: String | ||
|
|
||
| public init(rawValue: String) { | ||
| self.rawValue = rawValue | ||
| } | ||
|
|
||
| // Base ISQ quantities: https://en.wikipedia.org/wiki/International_System_of_Quantities#Base_quantities | ||
| case Amount | ||
| case Current | ||
| case Length | ||
| case Mass | ||
| case Temperature | ||
| case Time | ||
| case LuminousIntensity | ||
| public static let amount = Quantity(rawValue: "Amount") | ||
| public static let current = Quantity(rawValue: "Current") | ||
| public static let length = Quantity(rawValue: "Length") | ||
| public static let mass = Quantity(rawValue: "Mass") | ||
| public static let temperature = Quantity(rawValue: "Temperature") | ||
| public static let time = Quantity(rawValue: "Time") | ||
| public static let luminousIntensity = Quantity(rawValue: "LuminousIntensity") | ||
|
|
||
| // Extended SI Units: https://en.wikipedia.org/wiki/Non-SI_units_mentioned_in_the_SI | ||
| case Angle | ||
| case Data | ||
| public static let angle = Quantity(rawValue: "Angle") | ||
| public static let data = Quantity(rawValue: "Data") | ||
| } | ||
|
|
||
| // MARK: - Deprecated PascalCase aliases | ||
|
|
||
| // The previous `enum Quantity` spelled its cases in PascalCase. These aliases keep existing | ||
| // call sites compiling against the renamed lowerCamelCase properties (Swift API Design | ||
| // Guidelines) and can be removed in a future major release. | ||
| public extension Quantity { | ||
| @available(*, deprecated, renamed: "amount") | ||
| static let Amount = Quantity.amount | ||
| @available(*, deprecated, renamed: "current") | ||
| static let Current = Quantity.current | ||
| @available(*, deprecated, renamed: "length") | ||
| static let Length = Quantity.length | ||
| @available(*, deprecated, renamed: "mass") | ||
| static let Mass = Quantity.mass | ||
| @available(*, deprecated, renamed: "temperature") | ||
| static let Temperature = Quantity.temperature | ||
| @available(*, deprecated, renamed: "time") | ||
| static let Time = Quantity.time | ||
| @available(*, deprecated, renamed: "luminousIntensity") | ||
| static let LuminousIntensity = Quantity.luminousIntensity | ||
| @available(*, deprecated, renamed: "angle") | ||
| static let Angle = Quantity.angle | ||
| @available(*, deprecated, renamed: "data") | ||
| static let Data = Quantity.data | ||
| } | ||
|
|
||
| // MARK: - CustomStringConvertible | ||
|
|
||
| extension Quantity: CustomStringConvertible { | ||
| // Preserve the enum's behavior: interpolating a Quantity yields its raw value | ||
| // (e.g. "Length"), not the synthesized struct description. | ||
| public var description: String { rawValue } | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Swift API Design Guidelines: static properties should be lowerCamelCase.
This is a source-breaking change on existing call sites ( @available(*, deprecated, renamed: "length")
public static let Length = Quantity(rawValue: "Length")
public static let length = Quantity(rawValue: "Length")Either way, leaving public static properties in PascalCase in a Swift library is a lint error and will generate warnings for callers on newer toolchains. Generated by Claude Code |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing
CustomStringConvertible— this is a silent behavioral regression.With the
enum,"\(Quantity.Length)"(andString(describing:)) printed"Length". With the struct it will now print"Quantity(rawValue: \"Length\")"— the default struct description. Any code that logs or displays aQuantityvalue (or a[Quantity: Int]dictionary) will silently change its output.Please add:
This restores the prior behaviour and is consistent with how
Notification.Nameprints its string value.Generated by Claude Code