Skip to content

[CodeSynthesis] Improve synthesized Decodable.init(from:) for enums #41674

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
merged 1 commit into from
Mar 11, 2022

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Mar 4, 2022

  • Don't use unsafelyUnwrapped which is usually not recommended
  • Don't access container.allKeys multiple times as it's a computed property

Before:

  guard container.allKeys.count == 1 else { throw ... }
  switch container.allKeys.first.unsafelyUnwrapped { ... }

After:

  var allKeys = ArraySlice(container.allKeys)
  guard let onlyKey = allKeys.popFirst(), allKeys.isEmpty else { throw ... }
  switch onlyKey { ... }

rdar://89150202

@rintaro
Copy link
Member Author

rintaro commented Mar 4, 2022

@swift-ci Please test

@rintaro rintaro force-pushed the sema-codablesynth-rdar89150202 branch from 674880b to d2ae76b Compare March 5, 2022 00:47
@rintaro
Copy link
Member Author

rintaro commented Mar 5, 2022

@swift-ci Please test

@louisdh louisdh self-requested a review March 5, 2022 04:08
guard container.allKeys.count == 1 else {
var allKeys = ArraySlice(container.allKeys)

guard let onlyKey = allKeys.popFirst(), allKeys.isEmpty else {
Copy link
Contributor

@louisdh louisdh Mar 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!

I was confused at first seeing allKeys.isEmpty. Then I realized it's mutating allKeys with the popFirst() (I initially just read it as accessing the first key, not removing it from the array).

Seems like a much more efficient (and safe) way for checking that's there's only 1 key. 😃

Copy link
Contributor

@louisdh louisdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Since this implementation is exposed to users, we want to make it nice.

* Don't use 'unsafelyUnwrapped' which is usually not recommended to use
* Don't access 'container.allKeys' multiple times as it's a computed
  property

rdar://89150202
@rintaro rintaro force-pushed the sema-codablesynth-rdar89150202 branch from d2ae76b to 1cfffb8 Compare March 8, 2022 23:56
@rintaro
Copy link
Member Author

rintaro commented Mar 8, 2022

@swift-ci Please smoke test

@rintaro rintaro requested review from airspeedswift and removed request for louisdh March 8, 2022 23:57
Copy link
Member

@airspeedswift airspeedswift left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, thanks!

@rintaro rintaro merged commit 30a7ec1 into swiftlang:main Mar 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants