When did optional protocol methods become IUOs?

I just noticed that optional protocol methods are treated as implicitly-unwrapped optional values. I could have sworn they used to be full optionals. I can’t find any accepted swift-evolution proposal about this. Am I misremembering or did this really change at some point? And if so, why? Having them as IUO seems rather dangerous.

Are you perhaps using a slightly-old nightly build from master? If so, this PR is probably relevant: https://github.com/apple/swift/pull/14692

No this is with Swift 4.0.3, though I also confirmed that the latest Swift master snapshot has the same behavior.

I see this behavior on 4.0 and 4.1 (beta), which is what I’d expect:

@objc protocol Foo {
    @objc optional func bar()

@objc class Baz: NSObject, Foo {
    func bar() {}

let b = Baz()
let f: Foo = Baz()

b.bar() // OK
f.bar() // error: value of optional type '(() -> ())?' not unwrapped; did you mean to use '!' or '?'?
f.bar!() // OK
f.bar?() // OK

The first call to bar kinda looks like IUO behavior, but I’d expect that the compiler is just looking directly at Baz and seeing that it’s there.

Interesting. The autocomplete dropdown certainly thinks it's an IUO


But you're right, I can't actually directly invoke it. I didn't realize that before because I was working with a protocol & class imported from Obj-C, which has bugs around this (see SR-7067).

I'll file a bug about the autocomplete dropdown.

Edit: Filed as SR-7069.

I agree with you. I think that optional protocol methods should be optionals rather than IUO.

to me an IUO means "the object exists, otherwise crash"
While optional means, it could exist or it could not exist.

option protocol methods seems to align better with ‘?’ optional.

I dislike it when Xcode autocompletes bar(). When it should be using bar?() especially since we do not know if the method was implemented or not.

+1, but I don’t know how plausible this is given our source compatibility goals. @Douglas_Gregor, do you have an sense for this? What do you think about IOU vs strong optionals for optional protocol methods?