> The problem is that NSNull is in fact rare in Cocoa. I couldn't agree more. I think this proposal is quite dangerous for (often legacy) Objective C codebases where checking for NSNull is not common practice. Since Optionals are not a construct in Objective C, I don't see any reason for why Objective C Collection types would be populated with Optionals unless it's unintentional.
I'm not sure whether it's possible, but an ideal solution would be if the Swift Compiler can give a type error when passing an Optional to an Objective C Collection type. On Thu, Aug 25, 2016 at 9:27 AM, Greg Parker via swift-evolution < [email protected]> wrote: > > On Aug 23, 2016, at 3:36 PM, Douglas Gregor via swift-evolution < > [email protected]> wrote: > > Proposed solution > > When an Optional<T> value is bridged to an Objective-C object, if it > contains some value, that value should be bridged; otherwise, NSNull or > another sentinel object should be used. > > I don't think I like this. > > Let me verify my understanding. If I have this: > > // imported from ObjC > func f(with object: Any) > > let s: String? = nil > f(s) > > then at runtime it will call > f([NSNull null]) > ? > > The problem is that NSNull is in fact rare in Cocoa. They are used in the > Foundation containers and almost nowhere else. Passing NSNull into almost > any API is going to do something confusing at runtime. If you're lucky you > get a prompt error "-[NSNull something]: unrecognized selector". If you're > not lucky you'll get that error somewhere much later, or something even > less obviously related to NSNull and your call site. That sounds like the > wrong outcome for developers who are confused or careless or unaware of an > optional. > > > -- > Greg Parker [email protected] Runtime Wrangler > > > > _______________________________________________ > swift-evolution mailing list > [email protected] > https://lists.swift.org/mailman/listinfo/swift-evolution > >
_______________________________________________ swift-evolution mailing list [email protected] https://lists.swift.org/mailman/listinfo/swift-evolution
