2015-12-30 22 views
6

Ecco il mio codice:Gli inizializzatori da tipi generici non saranno ereditati in swift?

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) 

questo viene a mancare la compilazione e grida tale errore:

repl.swift:9:9: error: 'C' cannot be constructed because it has no accessible initializers 

Il seguente codice può essere compilato.

public class A { 
    public init(n : Int) { 
     print("A") 
    } 
} 
public class B : A { 
} 
public class C : B { 
} 
let x = C(n: 123) 

Non dovrebbe tipi requisito specificato initializers tipi generici essere ereditato?

======== aggiuntive Di seguito =======

“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.

E questo

“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.”

Quando esaminando il primo codice, sottoclasse B doesn 't definire eventuali inizializzatori designati, dovrebbe ereditare automaticamente tutti i suoi inizializzatori designati superclasse, quelli da A<Int>. Ma in realtà non è quello che sembra collegato a me.

+1

Anche qui osservato: http://stackoverflow.com/questions/31040044/swift-generic-superclass-init-not-accesible-when-constructing-its-subclass. –

+0

qualcuno ha depositato un biglietto in swift jira https://bugs.swift.org/projects/SR/issues/SR-416 –

+0

@AnthonyKong Sì, sono io. – AntiMoron

risposta

1

Che ne dici ?? Provo ad usare il codice override e super.init, non è un errore. Penso che non devi avviare la funzione con tipi generici.

cercano di mettere in funzione override init classe B e classe C. simile a questa,

public override init(n:Int) { super.init(n: n) }

0

Il codice non riuscendo ora compila (dal Swift 3). Non si fa menzione di questo cambiamento nello , quindi posso solo supporre che si sia trattato di un bug.