Why does second initialiser in base class break compilation? - swift

Given a base class, a derived class and an extension with a convenience initializer, the compiler throws an error if a second initializer is added to the base class like in the following sscce
#!/usr/bin/env swift
class A {
required init(a : Int){}
init(b: Int){} // when removing this initializer everything works fine
}
class B: A {
required init(a : Int){ super.init(a: a) }
}
extension A {
convenience init(c : Int) { self.init(a: c) }
}
let b: B = B(c: 1)
With the two initializers in the base class the following error is thrown:
... error: incorrect argument label in call (have 'c:', expected 'a:')
let b: B = B(c: 1)
^~
a
Apart from an error message which is not very helpful in this case, I am not quite sure if this is expected behaviour or a bug in swift.
Swift version info:
Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5)
Target: x86_64-apple-darwin18.5.0

From Automatic Initializer Inheritance:
Assuming that you provide default values for any new properties you introduce in a subclass, the following two rules apply:
Rule 1
If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers.
Rule 2
If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers.
Without init(b:) in class A, class B automatically inherits init(c:) from the extension and everything works.
But when you add init(b:) to class A, then class B no longer follows either rule and therefore class B no longer automatically inherits the init(c:) from the extension. This results in the error since class B now only has the one init(a:) initializer and no others.
You can fix the problem by overriding init(b:) in class B. With that in place, class B again automatically gets the init(c:) from the extension.

Related

Missing argument for parameter 'from' in call when creating instance of Codable class

I am trying to create a new instance of a codable class, but I am not sure how to:
var x = Articles();
gives me the following error:
Missing argument for parameter 'from' in call
class Articles: Codable {
let value: [Article]?
}
I don't understand since this is a parameterless class. I have no idea what the from parameter is all about.
I don't understand since this is a parameterless class. I have no idea
what the from parameter is all about.
I get no error when I run the following:
class Articles: Codable {
let value: [Articles]?
init() {
value = nil
print("in init()")
}
}
var x = Articles()
output:
in init()
And without init():
class Articles: Codable {
let value: [Articles]?
// init() {
// value = nil
// print("in init()")
// }
}
var x = Articles() //Missing argument for parameter 'from' in call
First, read this:
Automatic Initializer Inheritance
As mentioned above, subclasses do not inherit their superclass
initializers by default. However, superclass initializers are
automatically inherited if certain conditions are met. In practice,
this means that you do not need to write initializer overrides in many
common scenarios, and can inherit your superclass initializers with
minimal effort whenever it is safe to do so.
Assuming that you provide default values for any new properties you
introduce in a subclass, the following two rules apply:
Rule 1 If your subclass doesn’t define any designated initializers, it
automatically inherits all of its superclass designated initializers.
If you look at the docs, Codable is a typealias for the Decodable protocol (a protocol is like an interface in java). Protocols specify functions that a class must implement if they adopt the protocol. Your Articles class adopts the Decodable protocol. However, the docs for Decodable say,
init(from: Decoder)
Creates a new instance by decoding from the given decoder. Required.
Default implementation provided.
Protocols can actually implement functions by using extensions, which are then inherited by the class adopting the protocol.
As a result, the only designated initializer for your class is the one defined in the Decodable protocol, which takes one argument--which you inherit according to Rule 1. On the other hand, when you explicitly define another designated initializer in your class that takes no arguments, then you will call that initializer when you provide no arguments.

Why child class inherit convenience initializer when overriding all designated initializer?

class Parent {
var a : Int
init(a : Int) {
self.a =a
}
convenience init() {
self.init(a : 10)
}
}
class Child : Parent {
var b : Int
override init(a : Int){
b = 10
super.init(a : a)
}
}
var child = Child() // i know that convenience initializer is inherited, but why??
child.a // 10
child.b // 10
i know that convenience initializer is inherited, but why??
Just I override all designated initializer? so why is it need?
This is the rule as described in The Swift Programming Language:
Rule 2
If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers.
Inheriting convenience initializers is not needed if clients of the subclass don’t want to use them (just like inheriting methods and properties is not needed if clients of subclasses don’t use them), but it is the natural thing to do in my opinion.
Inheriting all of the superclass’s API is the default for subclasses, so convenience initializers should be inherited as well if possible. Since convenience initializers are required to call a sibling designated initializer, the compiler can be sure that all properties will be properly initialized only if all designated initializers are overridden in the subclass.

Overriding superclass initializer in Swift

I've found numerous examples for using the Singleton pattern in Swift 3. I'm trying to use this method:
class Records: RailsData {
static let shared = Records()
private init() {}
...
}
When I do, I get the compiler error:
Overriding declaration requires an 'override' keyword
When I add the override keyword, it compiles and everything seems to work. Is the override needed because I'm subclassing RailsData? RailData is an abstract class in that it is not instantiated directly. I also didn't make it a singleton.
I'm trying to make sure I don't get bit later by using the override modifier...
You are not doing anything wrong ;) The override modifier is required because your RailsData superclass declares a designated initializer with the exact same signature:
class RailsData {
init() {...}
...
}
as such, the override modified is made necessary in your subclass. The same thing applies to inherited methods as well. From the The Swift Programming Language book:
When you write a subclass initializer that matches a superclass designated initializer, you are effectively providing an override of that designated initializer. Therefore, you must write the override modifier before the subclass’s initializer definition. This is true even if you are overriding an automatically provided default initializer.
As with an overridden property, method or subscript, the presence of the override modifier prompts Swift to check that the superclass has a matching designated initializer to be overridden, and validates that the parameters for your overriding initializer have been specified as intended.
Rest assured, you won't be bitten by this later on ;)
Motivating example. To see this initializer overriding in action, try this example:
class SuperClass {
init(x: Int) {
print("3. SuperClass.init(x: \(x))")
}
convenience init() {
print("1. SuperClass.init()")
self.init(x: 123) // Will be overridden by subclass.
}
}
class SubClass: SuperClass {
override init(x: Int) {
print("2. SubClass.init(x: \(x))")
super.init(x: x)
print("4. SubClass.init(x: \(x))")
}
}
// Calls inherited convenience initializer.
let sub = SubClass()
outputs:
SuperClass.init()
SubClass.init(x: 123)
SuperClass.init(x: 123)
SubClass.init(x: 123)
Abstract classes. By the way, there is no direct, linguistic support for abstract classes in Swift — nor in Objective-C — but at least Cupertino is thinking about it ;) Currently, such a concept is merely a soft convention used by frameworks authors, etc.
To add to #Paulo Matteo's comprehensive answer, this is most likely the fix you need:
override private init() {
super.init()
}

Generic Constraints and Initializer Inheritance in Swift

I am trying to call an initializer required by protocol A on a type that both conforms to A and is a subclass of C.
All is fine and good if C is a base class. But as soon as C subclasses another class, say B, it breaks.
Here's what I am talking about:
protocol A {
init(foo: String)
}
class B {
init() {}
}
class C: B {}
func makeSomething<T: A>() -> T where T: B {
return T(foo: "Hi")
}
That works. But if I change where T: B to where T: C, I get the following error: argument passed to call that takes no arguments. It will only allow me to call Bs init.
Why can't I call this initializer? I understand that the super class has its own designated initializers that must be called. But that will be enforced when someone actually writes the class that subclasses B and conforms to A. (E.g. implementing init(Foo: String) and calling super.init(bar: Int) inside).
Any help here would be greatly appreciated. Thanks!
Swift provides a default initializer for your base class if it has all properties initialized but not incase of Generics because Swift may think its properties has not been initialized.
So when you constraint your return type as
func makeSomething<T: A>() -> T where T: C
It requires initialization for class C as Swift cannot provide a default initializer.But if your remove the where clause everything works fine
func makeSomething<T: A>() -> T {
return T(foo:"string")
}
If you want to return return T(foo: "Hi") :
You are getting error because class C doesn't have initializer that accepts init(foo: String)
Change your class C as
class C: A {
required init(foo: String) {
}
}
which provides at least one initializer that accepts the argument type.
So, remember if you don't subclass There is already one initializer doing your job and you dont get error.

Initializers from generic types won't be inherited in swift?

Here is my code:
public class A<T : Any> {
public init(n : Int) {
print("A")
}
}
public class B : A<Int> {
}
public class C : B {
}
let x = C(n: 123)
This fails compilation and yells such error:
repl.swift:9:9: error: 'C' cannot be constructed because it has no accessible initializers
The following code can be compiled.
public class A {
public init(n : Int) {
print("A")
}
}
public class B : A {
}
public class C : B {
}
let x = C(n: 123)
Shouldn't requirement types specified generic types' initializers be inherited?
========Additional Below=======
“Superclass initializers are inherited in certain circumstances, but only when it is safe and appropriate to do so. For more information, see Automatic Initializer Inheritance below.”
---Apple Inc. “The Swift Programming Language (Swift 2)” iBooks.
And this
“However, superclass initializers are automatically inherited if certain conditions are met.”
“Assuming that you provide default values for any new properties you introduce in a subclass, the following two rules apply:”
Rule 1
“If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers.”
Rule 2
“If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers.”
When looking into the first code, subclass B doesn't define any designated initializers,it should automatically inherits all of its superclass designated initializers, those from A<Int>.But actually it didn't which seems wired to me.
How about that ?? I try to use override code and super.init, it 's not error. I think that you don't have to init function with generic types.
try to put override init function in class B and class C.
Look like this,
public override init(n:Int) {
super.init(n: n)
}
Your failing code now compiles (since Swift 3). There is no mention of this change in the Swift 3 Language changes, so I can only assume that this was a bug.