Last active
January 14, 2016 11:23
-
-
Save mitsuse/3b3bbab538d51e9252cc to your computer and use it in GitHub Desktop.
Example: Automatic initializer inheritance doesn't work for generic types.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node<T> { | |
let value: T | |
init(value: T) { | |
self.value = value | |
} | |
} | |
class Terminal<T>: Node<T> { | |
} | |
// OK | |
let node = Node(value: "node") | |
// error: 'Terminal<T>' cannot be constructed because it has no accessible initializers | |
let terminal = Terminal(value: "terminal") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node<T> { | |
let value: T | |
init(value: T) { | |
self.value = value | |
} | |
} | |
class Terminal<T>: Node<T> { | |
override init(value: T) { | |
super.init(value: value) | |
} | |
} | |
// OK | |
let node = Node(value: "node") | |
// OK | |
let terminal = Terminal(value: "terminal") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
let value: String | |
init(value: String) { | |
self.value = value | |
} | |
} | |
class Terminal: Node { | |
} | |
// OK | |
let node = Node(value: "node") | |
// OK | |
let terminal = Terminal(value: "terminal") |
Automatic initializer inheritance
...
Rule 1
If your subclass doesn’t define any designated initializers,
it automatically inherits all of its superclass designated initializer.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.Automatic Initializer Inheritance - The Swift Programming Language
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[SR-416] Automatic initializer inheritance doesn't work for generic types. - Swift