[SR-11996] Warn when using immutable property with initial value in Codable type #54430
Labels
Codable
Area → standard library: `Codable` and co.
compiler
The Swift compiler in itself
diagnostics QoI
Bug: Diagnostics Quality of Implementation
improvement
Additional Detail from JIRA
md5: b4a7e5afcaedc2fa993c06d396d468ee
Issue Description:
If we have an immutable property with an initial value in a Codable type, then decoding the type will not set the value of the property to the decoded value. For example:
This is expected, because
bar
's value cannot be overwritten, however combined with the "magical" behaviour of Codable, this can be very confusing to many people.We should offer a warning, along the lines of
property 'foo' will not be decoded because it's immutable with an initial value; explicitly define a CodingKeys enum {with|without} the property
, along with a fix-it to make the property mutable. This will make the behaviour more explicit to the users.The {with|without} part depends on whether the type is only
Decodable
, in which case the property should not be included in the enum. If it'sEncodable
(or both Encodable/Decodable) then the property should be included.The text was updated successfully, but these errors were encountered: