Uploaded image for project: 'Swift'
  1. Swift
  2. SR-7562

@discardableResult is disregarded for a required init method

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:
    • Environment:

      swift --version

      Apple Swift version 4.1 (swiftlang-902.0.48 clang-902.0.37.1)

      Target: x86_64-apple-darwin17.5.0

      Description

      My problem is sort of related to the following issue: SR-2198

      Consider the following example:

      protocol HasInitMethod {
          associatedtype Input
          init(input: Input)
      }
      
      class Foo<Input>: HasInitMethod {
          @discardableResult required init(input: Input) {
              //empty for now
          }
      }
      
      class Bar: Foo<Int> {
      }
      

      If I instantiate Foo, with its init method marked with @discardableResult, then the compiler does a good job of not warning me about the unused result.

      However, if I instantiate Bar, who inherits its init method from Foo, I will get a warning:

      Foo(input :10) //no warnings, nice!
      Bar(input: 10) //warning: Result of 'Bar' initializer is unused

      I believe this to be a bug and not a feature since Bar can and should inherit its init method from Foo to be able to get the same effect as Foo has.

        Attachments

          Activity

            People

            Assignee:
            vguerra Victor Guerra
            Reporter:
            Vandad Vandad Nahavandipoor
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: