#selector and Void methods


(Brent Royal-Gordon) #1

So, today is day 2 of Swift 2.2 being in the hands of mere mortals. This evening, I walked into my weekly NSCoder Night meeting and talked to a pretty experienced developer struggling with this:

  class EditEventViewController: UIViewController {
    ...
    @IBAction func cancel(sender: AnyObject?) {
      cancel()
    }
    func cancel() {
      // actual canceling logic is here
    }
  }
  
  class EditEventContainerViewController: UIViewController {
    ...
    func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) {
      switch segue.identifier {
        ...
        let cancelButton = UIBarButtonItem(barButtonSystemItem: .Cancel, target: nil, action: nil)
        cancelButton.target = vc
        cancelButton.action = #selector(vc.cancel) // This is the problem line
        ...
      }
    }
  }
    
`vc.cancel` was, of course, ambiguous, and the SE-0021 `vc.cancel(_:)` syntax can't select a Void function. I had to explain how to use `as Void -> Void` to select the right version.

This is merely an anecdote, but I think it may end up being an issue.

···

--
Brent Royal-Gordon
Architechies


(Joe Groff) #2

Thanks for reporting this. I also saw this come up at least one other place:

https://twitter.com/bendodson/status/712208662933200896

I wonder if it's worth making #selector(vc.cancel()) work. What do you think, Doug?

-Joe

···

On Mar 22, 2016, at 8:49 PM, Brent Royal-Gordon via swift-evolution <swift-evolution@swift.org> wrote:

So, today is day 2 of Swift 2.2 being in the hands of mere mortals. This evening, I walked into my weekly NSCoder Night meeting and talked to a pretty experienced developer struggling with this:

  class EditEventViewController: UIViewController {
    ...
    @IBAction func cancel(sender: AnyObject?) {
      cancel()
    }
    func cancel() {
      // actual canceling logic is here
    }
  }
  
  class EditEventContainerViewController: UIViewController {
    ...
    func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) {
      switch segue.identifier {
        ...
        let cancelButton = UIBarButtonItem(barButtonSystemItem: .Cancel, target: nil, action: nil)
        cancelButton.target = vc
        cancelButton.action = #selector(vc.cancel) // This is the problem line
        ...
      }
    }
  }
    
`vc.cancel` was, of course, ambiguous, and the SE-0021 `vc.cancel(_:)` syntax can't select a Void function. I had to explain how to use `as Void -> Void` to select the right version.

This is merely an anecdote, but I think it may end up being an issue.


(Jordan Rose) #3

Very strong +1; I consider it a bug that we didn't have this already. It was reported as SR-1016 <https://bugs.swift.org/browse/SR-1016>.

Jordan

···

On Mar 23, 2016, at 10:11, Joe Groff via swift-evolution <swift-evolution@swift.org> wrote:

On Mar 22, 2016, at 8:49 PM, Brent Royal-Gordon via swift-evolution <swift-evolution@swift.org> wrote:

So, today is day 2 of Swift 2.2 being in the hands of mere mortals. This evening, I walked into my weekly NSCoder Night meeting and talked to a pretty experienced developer struggling with this:

  class EditEventViewController: UIViewController {
    ...
    @IBAction func cancel(sender: AnyObject?) {
      cancel()
    }
    func cancel() {
      // actual canceling logic is here
    }
  }
  
  class EditEventContainerViewController: UIViewController {
    ...
    func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) {
      switch segue.identifier {
        ...
        let cancelButton = UIBarButtonItem(barButtonSystemItem: .Cancel, target: nil, action: nil)
        cancelButton.target = vc
        cancelButton.action = #selector(vc.cancel) // This is the problem line
        ...
      }
    }
  }
    
`vc.cancel` was, of course, ambiguous, and the SE-0021 `vc.cancel(_:)` syntax can't select a Void function. I had to explain how to use `as Void -> Void` to select the right version.

This is merely an anecdote, but I think it may end up being an issue.

Thanks for reporting this. I also saw this come up at least one other place:

https://twitter.com/bendodson/status/712208662933200896

I wonder if it's worth making #selector(vc.cancel()) work. What do you think, Doug?


(Douglas Gregor) #4

Yes, I agree.

  - Doug

···

On Mar 23, 2016, at 10:14 AM, Jordan Rose via swift-evolution <swift-evolution@swift.org> wrote:

On Mar 23, 2016, at 10:11, Joe Groff via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

On Mar 22, 2016, at 8:49 PM, Brent Royal-Gordon via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

So, today is day 2 of Swift 2.2 being in the hands of mere mortals. This evening, I walked into my weekly NSCoder Night meeting and talked to a pretty experienced developer struggling with this:

  class EditEventViewController: UIViewController {
    ...
    @IBAction func cancel(sender: AnyObject?) {
      cancel()
    }
    func cancel() {
      // actual canceling logic is here
    }
  }
  
  class EditEventContainerViewController: UIViewController {
    ...
    func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) {
      switch segue.identifier {
        ...
        let cancelButton = UIBarButtonItem(barButtonSystemItem: .Cancel, target: nil, action: nil)
        cancelButton.target = vc
        cancelButton.action = #selector(vc.cancel) // This is the problem line
        ...
      }
    }
  }
    
`vc.cancel` was, of course, ambiguous, and the SE-0021 `vc.cancel(_:)` syntax can't select a Void function. I had to explain how to use `as Void -> Void` to select the right version.

This is merely an anecdote, but I think it may end up being an issue.

Thanks for reporting this. I also saw this come up at least one other place:

https://twitter.com/bendodson/status/712208662933200896

I wonder if it's worth making #selector(vc.cancel()) work. What do you think, Doug?

Very strong +1; I consider it a bug that we didn't have this already. It was reported as SR-1016 <https://bugs.swift.org/browse/SR-1016>.