New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-6655] x == nil
works by Optional promotion if x
is Any
#49204
Comments
I don't think this is specific to key paths, this is an overload resolution problem because of the weird special-case |
@swift-ci create |
That's true, but we could make that work if we wanted to. I'm just not sure whether we want to. |
Sorry I wasn't clear. I'm trying to say it does work today, and it apparently also works by accident when `x` isn't statically Optional. |
It prints |
I see what you're saying now. That's not really correct by the current design, though, since the operation is receiving an |
I'd normally agree, but the fact that we bridge nil optionals to NSNull in non-optional contexts is what made me not sure. But I don't feel strongly about it. |
Looking inside the Any isn't something we express in the language today. The runtime could do it with some tricks, but it feels like peeking inside the Any for this operation would create more surprises elsewhere, if one expects other parts of the language to look through an Any. We have the warning due to the potential for surprise here. Personally, I don't think we should make any other changes. |
Comment by Cory Alder (JIRA) I was attempting to use keyPaths to write a validator, that would take an object and return a list of keypaths that have nil values. But I get this weird behavior: struct Person {
let name: String?
let date: Date?
}
let person = Person(name: "hello", date: nil)
let paths = [\Person.name, \Person.date]
let resolved = paths.map { person[keyPath: $0] }
// resolved is: [Optional("hello"), nil]
let filtered = resolved.filter { $0 == nil }
// filtered is: [] Namely, I would expect `filtered` to contain one nil value, instead of no values. It looks like this is because `person[keyPath: $0]` is returning `Any` instead of `Any?` but disambiguating those does not seem possible? |
The compiler is just looking at an array containing KeyPath<Person, String?> and KeyPath<Person, Date?> and deciding that the best common type is PartialKeyPath<Person>. Swift doesn't have the notion of covariant generic parameters required for the compiler to even consider using KeyPath<Person, Any?> instead. |
Environment
macOS High Sierra 10.13.1 (17B1003), Xcode 9.2 (9C40b), Swift 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)
Additional Detail from JIRA
md5: 120990bfcfaeff313fd7e17d9d956592
Issue Description:
A comparison with nil of the value of an Optional property accessed through a PartialKeyPath doesn't return true if the value is nil. Example:
The text was updated successfully, but these errors were encountered: