-
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
Merged
NeedleInAJayStack
merged 3 commits into
NeedleInAJayStack:main
from
DarkAndHungryGod:feat/extensible-quantity
Jul 3, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } | ||
| } | ||
|
NeedleInAJayStack marked this conversation as resolved.
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.