Rename Encodable.CodingKeys?

The documentation on enumerations clearly reads,

Give enumeration types singular rather than plural names, so that they read as self-evident:

var directionToHead = CompassPoint.west

It looks like CodingKeys is an exception to this. New adopters of Swift will encounter it and suppose that their own enumeration types should likewise have plural names.

The name CodingKey is already used as a protocol. We could rename either CodingKey or CodingKeys.

What do you think?

6 Likes

You're quite right that CodingKeys is an exception. This would have been a valid point to bring up during review of the Swift Evolution proposal for Codable. In hindsight it may have been appropriate to name the type CodingKey and the protocol CodingKeyProtocol.

Once features have been designed and implemented, however, the threshold for reconsidering the design is considerably higher. This is why it is important that proposals are so carefully considered the first time.

I can see absolutely no circumstance in which CodingKeys or CodingKey could be renamed now.

8 Likes

Great catch! +1 from me. With a simple fix-it to update the existing code, I see no downsides.

1 Like

I agree with @xwu even though I don't like the CodingKeys name because in most cases we use an enum and a single value of that enum does not represent multiple coding keys but a single one, therefore the name should always have been CodingKey, not its plural form.

I don't see this as a regular enum, but rather as a hack that hopefully will be replaced in the near future (say... not more than five years from now ;-) - so I don't think it's justified to spend time on fixing minor inconsistencies.

2 Likes

Replaced with what? Was there any discussion back then with a future direction of this design?

I could see CodingKeys being improved / changed in favor of a property wrapper maybe?

@KeyedBy("avatar_url") let avatar: URL

FYI this is something I just made up, not something that has been discussed AFAIK

2 Likes

I guess most ideas have been discussed before :slight_smile: - and for this topic, a new thread has been started recently: Pre-Pitch Codable Customization using PropertyWrappers

Maybe we'll see some tools that fit even better in the future, though.

2 Likes
Terms of Service

Privacy Policy

Cookie Policy