Skip to content

Instantly share code, notes, and snippets.

@mitsuse
Last active January 14, 2016 11:23
Show Gist options
  • Save mitsuse/3b3bbab538d51e9252cc to your computer and use it in GitHub Desktop.
Save mitsuse/3b3bbab538d51e9252cc to your computer and use it in GitHub Desktop.
Example: Automatic initializer inheritance doesn't work for generic types.
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")
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")
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")
@mitsuse
Copy link
Author

mitsuse commented Jan 14, 2016

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