-
Notifications
You must be signed in to change notification settings - Fork 190
[Proposal] Conform UUID to LosslessStringConvertible #1317
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
Open
jevonmao
wants to merge
2
commits into
swiftlang:main
Choose a base branch
from
jevonmao:proposal_uuid_0027
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Conform `UUID` to `LosslessStringConvertible` | ||
|
||
* Proposal: [SF-0027](0027-lossless-string-convertible.md) | ||
* Authors: [Jevon Mao](https://github.com/jevonmao) | ||
* Review Manager: TBD | ||
* Status: **Awaiting Review** | ||
* Implementation: [swiftlang/swift-foundation#1303](https://github.com/swiftlang/swift-foundation/pull/1303) | ||
* Review: ([pitch](https://forums.swift.org/t/pitch-conform-uuid-to-losslessstringconvertible/80084/1)) | ||
|
||
## Introduction | ||
|
||
This proposal adds conformance of `UUID` to the `LosslessStringConvertible` protocol. This enables UUIDs to be created from and converted to their canonical string representations in a lossless and bidirectional way using standard Swift protocols. | ||
|
||
## Motivation | ||
|
||
Swift's `UUID` type is a commonly used identifier type that already supports converting to and from strings via `uuidString` and `init?(uuidString:)`. However, it does not currently conform to the standard library's `LosslessStringConvertible` protocol, which is surprising given that the conversion is already fully supported and round-trippable. | ||
|
||
This omission prevents developers from writing generic code that relies on this protocol. For example, web frameworks like Vapor have implemented their own conformance in order to enable route decoding via string interpolation. | ||
|
||
Adding this conformance would standardize this behavior in the Swift Foundation library, remove the need for third-party workarounds, and improve UUID's interoperability in generic APIs. | ||
|
||
## Proposed solution | ||
|
||
Extend `UUID` to conform to `LosslessStringConvertible` by implementing: | ||
|
||
```swift | ||
extension UUID: LosslessStringConvertible { | ||
public init?(_ description: String) { | ||
self.init(uuidString: description) | ||
} | ||
|
||
public var description: String { | ||
return self.uuidString | ||
} | ||
} | ||
``` | ||
|
||
This conformance ensures that a valid UUID string can initialize a `UUID`, and that the `description` property returns a string that can reconstitute the same value, satisfying the requirements of `LosslessStringConvertible`. | ||
|
||
## Detailed design | ||
|
||
A new test was added to validate round-trip conformance and nil-initialization on invalid input: | ||
|
||
```swift | ||
func test_UUIDLosslessStringConvertible() { | ||
let originalString = "E621E1F8-C36C-495A-93FC-0C247A3E6E5F" | ||
let uuidFromString = UUID(originalString) | ||
XCTAssertNotNil(uuidFromString) | ||
XCTAssertEqual(uuidFromString?.description, originalString) | ||
|
||
let uuid = UUID(uuidString: originalString)! | ||
XCTAssertEqual(uuid, UUID(uuid.description)) | ||
|
||
let invalidUUID = UUID("not-a-uuid") | ||
XCTAssertNil(invalidUUID) | ||
} | ||
``` | ||
|
||
## Source compatibility | ||
|
||
This proposal is purely additive and does not break source compatibility. | ||
|
||
|
||
## Implications on adoption | ||
|
||
Adopting this change is safe and beneficial without ABI stability concerns. | ||
|
||
## Future directions | ||
|
||
- Consider reviewing similar Foundation types for conformance to `LosslessStringConvertible` where it makes sense. | ||
|
||
## Alternatives considered | ||
|
||
- **Do nothing:** Keep current API, but will cause inconsistencies across Swift APIs and third-party workarounds. | ||
|
||
## Acknowledgments | ||
|
||
Thanks to [Stephen Celis](https://forums.swift.org/u/stephencelis) for initiating this discussion on the Swift Forums and [Sindre Sorhus](https://github.com/sindresorhus) for opening an issue on this adoption. | ||
|
||
|
||
|
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.
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.
Let’s be sure to include availability annotations here, too. I assume FoundationPreview 6.2 for now.