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-5355] "Cannot override non-throwing initializer with throwing initializer" #47929

Open
CharlesJS opened this issue Jul 2, 2017 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@CharlesJS
Copy link

Previous ID SR-5355
Radar None
Original Reporter @CharlesJS
Type Bug
Environment

Xcode 9 beta 2

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

md5: 2605a0fa960b3efb9453ee44061951fe

is duplicated by:

  • SR-5371 Allow subclass have throwing initializer when superclass don't

Issue Description:

So, suppose you want to make a subclass of, say, InputStream, and initialize it to a URL, like so:

class InputStream {
    init(url: URL) throws {
        ...
    }
}

An error is returned: "Cannot override non-throwing initializer with throwing initializer." This error seems spurious:

  • Initializers aren't inherited, so why does it matter if an initializer has the same signature as one of the superclass's? None of the superclass initializers can even be called outside of a 'super' call in one of our initializers, so for the most part they're living in entirely independent fiefdoms.

  • This isn't really even an override; init?(url🙂 translates to initWithURL:, whereas init(url🙂 throws translates to initWithURL:error:. Two entirely different signatures, and a pattern that would have worked without incident in Objective-C.

The unfortunate end result of this situation is that one either has to lose error information in failure conditions, or give the initializer a suboptimal name.

@belkadan
Copy link
Contributor

belkadan commented Jul 4, 2017

The override part is still significant because of convenience initializers. If a class overrides all designated initializers in its superclass, it inherits any convenience initializers. In this situation, we'd probably want some kind of nonoverride attribute or modifier that acknowledges the similarity between the two initializers but chooses to ignore it. (This kind of answer would have to go through the Swift Evolution Process.)

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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

2 participants