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-5122] Codable conformance compilation error with required initializers #3849
Comments
In the case of classes, we might want to always be willing to call a superclass's default constructor if it's not |
Comment by Kashif Shaikh (JIRA) The same problem can be manifested without required init: class TestObject {
let x: String
init(x:String) {
self.x = x
}
}
class TestCodable : TestObject, Codable {
let y: String
let z: String
init(x:String, y:String, z:String) {
self.y = y
self.z = z
super.init(x:x)
}
} The compiler fails with: <unknown>:0: error: super.init isn't called on all paths before returning from initializer If I manually add the decodable methods, it compiles successfully. class TestCodable : TestObject, Codable {
let y: String
let z: String
init(x:String, y:String, z:String) {
self.y = y
self.z = z
super.init(x:x)
}
required init(from decoder: Decoder) throws {
let x = "decode x"
let y = "decode y"
let z = "decode z"
self.y = y
self.z = z
super.init(x:x)
}
} Because the code generated for the Decodable init method can't know for certain which super.init method to call. Normally we could code our model objects such that we don't have inheritance, which kind of solves the problem. But many of us are using Realm, and when we add Codable to our Realm model objects, the compiler fails with the same error. |
I've thought some more about it and I think it would be really beneficial for the compiler to not error out when the super class has an argument-less initialiser. It's going to be such a common case that it would be really a pity to force users to implement their own initializer just to call |
@swift-ci Create |
I think the problem can be expressed far more compactly: class Superclass : Codable {}
class Subclass : Superclass {
var prop : String?
init(prop:String) {
self.prop = prop
}
} This gives:
The problem is that giving the subclass a designated initializer has cut off inheritance of the Codable injected |
This was actually fixed a while back before the Swift 4.0 release. |
@mattneub That looks like a separate issue. Let me look into that. |
@itaiferber Sorry about that: would you like me to submit it separately? I couldn't find anything comparable and this looked very close. |
@mattneub Yeah, that's not class Super {
required init() {}
}
class Sub : Super {
var prop: String?
init(prop: String) {
self.prop = prop
}
} produces Untitled 2.swift:10:1: error: 'required' initializer 'init()' must be provided by subclass of 'Super'
}
^
Untitled 2.swift:2:14: note: 'required' initializer is declared in superclass here
required init() {} If your superclass has a |
I know, but this undoes all the good done by Codable's injected |
@mattneub That might be possible, but I'm not sure if it would be worth it. That being said, I think that the compiler diagnostic could certainly be improved. Given that it took me a sec to figure out why you were getting that message, I think we can do better (e.g. "note: subclass initializer 'init(prop: String)' prevented inheritance of 'required init()'; 'required init()' must be overridden"). That should be filed separately, though, if you don't mind. 🙂 |
@itaiferber will do. |
@mattneub Thanks! 🙂 |
Implement a custom Data.Iterator
Additional Detail from JIRA
md5: c57b8fae77dfef6e627bfb9f5eb19530
relates to:
Issue Description:
The following piece of code results in a compilation error, most probably in the code generated by the Codable conformance. This seems to happen when the super class has a required initializer:
The text was updated successfully, but these errors were encountered: