How to make noise "result not used" warnings

In Swift2.2, I have an extension to Optionalthat that looks like this:

extension Optional {
    func ifNotNil<T>(_ closure:(Wrapped) -> T) -> T? {
        switch self {
        case .some (let wrapped):
            return closure(wrapped)
        case .none:
            return nil
        }
    }
}

It allows you to use code like

anImageView.image = self.something.ifNotNil { self.getImageFor($0) }

But sometimes I don’t care about the result:

myBSON["key"].string.ifNotNil {
    print($0}
}

In Swift2.2, it worked like a charm. But by releasing a new beta version of Xcode8 and converting it to Swift3, I get warnings wherever I make the second type. It is almost as if implied @warn_unused_result. Is this just an early beta bug? Or something that I can no longer do in Swift3? Or do I need something for a new fix in Swift3?

+4
source share
1 answer

You can cancel the result using:

_ = myBSON["key"].string.ifNotNil {
    print($0}
}

, :

extension Optional {

    @discardableResult func ifNotNil<T>(_ closure:(Wrapped) -> T) -> T? {
        switch self {
        case .some (let wrapped):
            return closure(wrapped)
        case .none:
            return nil
        }
    }
}

: SE-0047

+6

All Articles