This is not true here — any class which inherits from a class that adopts a protocol inherits that protocol conformance; that inheritance may conflict with other requirements on the class, though. This is actually exactly what leads to this behavior: SuperClass
inherits BaseClass
’s Codable
implementation directly as it cannot yet synthesize its own.
In any inheritance situation, if a subclass adds new properties which do not have default values, it cannot inherit its superclass’s initializers since those would not initialize said properties. One would need to add an initializer which explicitly initializes SuperClass.bar
, i.e., init(from:)
.
Essentially the following is happening:
class Foo { var a: Int; init() { a = 3 } }
class Bar : Foo { var b: String } // Class ‘Bar’ has no initializers
The reason the compiler cannot synthesize an implementation on behalf of SuperClass
is mostly due to two things:
-
Some amount of implementation detail (which is not a good answer)
-
There’s currently no way to indicate in any given class whether a subclass wants to inherit or re-synthesize conformance. This is a somewhat unique situation: in traditional inheritance, there are only two options — either you provide an overridden implementation of a method, or you don’t. Synthesis adds a third option in allowing neither you nor your superclass to provide that code. We would need new syntax to explicitly specify that you’d like to synthesize over the other two options, since doing nothing already indicates “inherit”.
There are thoughts about disambiguating the situation (e.g. redeclaring conformance to a synthesized protocol could indicate you’d like to re-synthesize instead of inherit, i.e.
class SuperClass : BaseClass, Codable {...}
which is currently disallowed due to being redundant), but nothing concrete yet.
There’s some amount of design work that needs to happen here that we plan on doing.