Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-1375] Availability of default values in initializer or generics in class definition may prevent swift from inheritance of initializer. #43984

Closed
swift-ci opened this issue Apr 30, 2016 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-1375
Radar None
Original Reporter nikita (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment

Apple Swift version 2.2 (swiftlang-703.0.18.1 clang-703.0.29)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 689b363b04e00a5ba10ec88c9241d8f5

duplicates:

  • SR-416 Automatic initializer inheritance doesn't work for generic types.

Issue Description:

According to Apple's documentation Swift does not necessary require override of initializer. In a following code example Bar inherits initializer of Foo:

class Foo {
  let value: Int
  init(value: Int = 5) {
    self.value = value
  }
}

class Bar: Foo {
}

Modifying code as below prevents inheritance of initializer.

class Foo<T> {
  let value: Int
  init(value: Int = 5) {
    self.value = value
  }
}

class Bar: Foo {
}

Here are an option how to define a class with generic that does not require override of designated initializer:

protocol FooProtocol {
    associatedtype T
}

class Foo<U>: FooProtocol {
    typealias T = U

    let value: Int
    init(value: Int, otherValue: T) {
        self.value = value
        self.otherValue = otherValue
    }
}

class Bar: Foo<Int> {
}

However there is another interesting observation of behavior. Defining initializer like following cause override requirement:

init(value: Int = 5) {
    self.value = value
}

The funny thing thing that adding one more parameter as following into such designated initializer cause this override requirement to disappear:

init(value: Int = 5, otherValue: T) {
    self.value = value
}
@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

1 participant