Inheritance of an initializer from a common class

I saw some discussions about this issue, but did not get a satisfactory explanation. Can someone tell me why this is not working?

class Parent<T> {

  var data:T

  init(data:T) {
    self.data = data
  }
}

class Child : Parent<Int> {}

let c = Child(data: 4)

The last line gives an error:

'Child' cannot be constructed because it has no accessible initializers

Do I really need to implement a call-only initializer super?

Edit:

, . Action, generics, , , Swift, , . (, CustomAction). init . , , , .

class Action<Input, Output> {

  var cachedOutput:Output?

  init(cachedOutput:Output?) {
    self.cachedOutput = cachedOutput
  }
}

protocol CustomInput {}
protocol CustomOutput {}

class CustomAction : Action<CustomInput, CustomOutput> {
}
+4
2

Swift 3. . Swift 3 Language changes, , .

0

, init.

class Parent<T> {

    var data:T

    init(data:T) {
        self.data = data
    }
}

class Child<T> : Parent<T> {
    override init(data: T) {
        super.init(data: data)
    }
}

let c = Child(data: 4)       // Child<Int>
let c2 = Child(data: "alfa") // Child<String>

...

// what is the type T ? it is undeclared!
class Child2: Parent2<T> {}
// how to specialize non-generic type Parent ? how to create it?
// i need an initializer in class Child3 ... Hm ...
class Child3: Parent<Int> {}

// cannot specialize non-generic type 'Parent'
class Child3: Parent<Int> {
    override init(data: Int) {
        super.init(data: data)
    }
}
// So, Child3 must be of the same specialized type as Parent!!

, ? !

class Parent<T> {

    var data:T

    init(data:T) {
        self.data = data
    }
}

class Child<Double> : Parent<String> {
    init(data: Double) {
        super.init(data: "\(data)")
    }
}

let c = Child(data: 4)       // Child<Int> !!!!!
let d = Child(data: true)    // Child<Bool> !!!

class Parent<T> {

    var data:T

    init(data:T) {
        self.data = data
    }
}

class Child: Parent<String> {
    init(data: Double) {
        super.init(data: "\(data)")
    }
}

let c = Child(data: 4)
print(c.dynamicType) // Child    :-)
+2

Source: https://habr.com/ru/post/1619697/


All Articles