Skip to content

Conversation

@sindresorhus
Copy link
Owner

Fixes #99


I don't expect to merge this anytime soon because of the system requirements and it being a breaking change, but I would like a review.


Why CodingKeyRepresentable instead of LosslessStringConvertible?

CodingKeyRepresentable is the better choice because:

  1. Designed for dictionary keys: CodingKeyRepresentable was specifically created to enable
    non-String/Int types as Codable dictionary keys. It's purpose-built for this exact use case.

  2. Better enum support: Enums with Int raw values get automatic CodingKeyRepresentable conformance from the standard library.

  3. Cannot support both: Swift explicitly forbids multiple conditional conformances of Dictionary to Serializable, even with different conditional bounds. We must choose one approach.

What works as dictionary keys?

  • String and Int (built-in)
  • Enums with String or Int raw values (automatic via RawRepresentable)
  • Custom types that manually conform to CodingKeyRepresentable

Types that are only LosslessStringConvertible (like Bool) won't work. However, these are uncommon as dictionary keys and using them often indicates a design issue.

Copy link
Collaborator

@hank121314 hank121314 left a comment

Choose a reason for hiding this comment

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

Many thanks for this 🙇!
Really LGTM!
Just a question and one nit 🙏

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.

Support CodingKeyRepresentable for DictionaryBridge?

2 participants