[swift-evolution] Swift's 'observe()' doesn't work for key paths with optionals?

William Shipley wjs at mac.com
Sat Jul 22 02:12:12 CDT 2017


When I try to observe a key path that includes an optional (“controller” in this case) in Swift, I get a compiler error saying ‘Type of expression is ambiguous without more context’ on the backslash in this statement:

        submodelObservation = observe(\.controller.submodel) { object, change in
    // code
        }


    @objc dynamic internal var controller: Controller?

in the the “Controller" class:

    @objc dynamic internal var submodel: SubmodelForImporting?


I’m not clear why this would be, but I admit I missed the discussion the first time it happened. If I make “Controller” non-optional the code compiles fine.

Certainly with traditional KVO you can observe key paths that have ‘nil’ values in them! Is this something stupid I’m doing, a compiler bug, or a design decision? I’m asking here because if it’s a design decision I’d like to appeal it, certainly.

-Wil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170722/21bea423/attachment.html>


More information about the swift-evolution mailing list