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
[SR-6875] Codable codegen blocked by subclass with designated initializer #49424
Comments
To loosely bring my comment from SR-5122 over — But, at the very least, I think that we can improve the diagnostic to make it clearer what's going on. Yes, you will still have to manually implement @belkadan Any thoughts? |
FWIW, even if |
I still think it's a trap that I really wish we could ask for synthesis somehow. |
Just to emphasize the point once again: it's not like you and I don't know how to write |
This could be a product of the same problem: https://stackoverflow.com/questions/48566443/implementing-codable-for-uicolor The OP expected to be able to make UIColor Codable but couldn't. |
@mattneub That's a different issue — you can't add import Foundation
extension NSString: Decodable {
public required init(from decoder: Decoder) throws {
self.init(string: "Hello!")
}
} produces Untitled 2.swift:4:21: error: designated initializer cannot be declared in an extension of 'NSString'; did you mean this to be a convenience initializer?
public required init(from decoder: Decoder) throws {
^
convenience
Untitled 2.swift:4:21: error: 'required' initializer must be declared directly in class 'NSString' (not in an extension)
public required init(from decoder: Decoder) throws {
~~~~~~~~ ^
Untitled 2.swift:4:21: error: initializer requirement 'init(from:)' can only be satisfied by a `required` initializer in the definition of non-final class 'NSString'
public required init(from decoder: Decoder) throws {
^ in the same way that import Foundation
extension NSString {
public required init<T>(_ foo: T) {
print("NSString.init<\(T.self)>(_:)")
self.init(string: "Hello!")
}
} produces Untitled 2.swift:4:21: error: designated initializer cannot be declared in an extension of 'NSString'; did you mean this to be a convenience initializer?
public required init<T>(_ foo: T) {
^
convenience
Untitled 2.swift:4:21: error: 'required' initializer must be declared directly in class 'NSString' (not in an extension)
public required init<T>(_ foo: T) {
~~~~~~~~ ^ The SO question just ommitted the relevant error there. |
Comment by Aaron Von Gauss (JIRA) I personally wouldn't describe this as a beginner issue, I think its more of a gap in the implementation. Logically, for any Type that is marked Decodable or Encodable either directly or indirectly through inheritance the compiler should have the potential to offer a default (codgen) implementation of CodingKeys, init(from🙂 and/or encode(to🙂 appropriately. The CodingKeys if present in the source or generated belong to the Type and should be private to that Type. Having the compiler evaluate and work with each individual Type I would think would produce the expected (desirable) behavior. |
Additional Detail from JIRA
md5: 8cbf28e52969ee8f5d0e47bab2efe5df
relates to:
Issue Description:
This issue arose as a complaint from a beginner on Stack Overflow:
https://stackoverflow.com/questions/48491971/how-to-declare-a-subclass-designated-initializer-with-more-parameters-than-super
The OP has misunderstood the problem, which is easily expressed as follows:
This gives:
error: 'required' initializer 'init(from:)' must be provided by subclass of 'Superclass'
The trouble is that giving the subclass a designated initializer has cut off inheritance of the Codable injected
init(from:)
. That's normal, but the trouble is that we are therefore forced to writeinit(from:)
manually in the subclass. That's ugly, and many beginners can't handle it at all.The injected
init(from:)
(performing codegen by way of a protocol extension) is a real time-saver, and opens Codable to the use of beginners. It seems a pity, therefore, that it should never be able to apply to a subclass of a Codable adopter. In effect, we're cutting off a huge set of object types from its benefits. I wonder whether the language could not be modified to make this work somehow.The text was updated successfully, but these errors were encountered: