-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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-12897] "\.self" not working with keypath-as-function - Examples in Key Path Expressions as Functions proposal don't compile #55343
Comments
@swift-ci create |
Just an update with what we are seeing on the 2020-05-22 master snapshot. struct User {
let email: String
}
let users = [User]()
let f3 = \User.email as (User) -> String // Cannot convert value of type 'KeyPath<User, String>' to type '(User) -> String' in coercion
users.map(f3)
[1, nil, 3, nil, 5].compactMap(\.self) // Cannot convert value of type 'WritableKeyPath<_, _>' to expected argument type '(Int?) throws -> ElementOfResult?'
// Cannot infer key path type from context; consider explicitly specifying a root type fix-it root
// Generic parameter 'ElementOfResult' could not be inferred
let x: [Int] = [1, nil, 3, nil, 5].compactMap(\.self) // Cannot convert value of type 'WritableKeyPath<_, _>' to expected argument type '(Int?) throws -> Int?'
// Cannot infer key path type from context; consider explicitly specifying a root type. |
On investigation, the |
In a near-release/5.3 Swift, I see better diagnostics but not better behavior:
The problem appears to be specific to
Whereas even simpler |
Comment by Michael Ozeryansky (JIRA) brentdax (JIRA User) Awesome, thanks. I'm glad there's a slight workaround if I really want the syntax, good find. |
Looks like duplicates: https://bugs.swift.org/browse/SR-12387 |
Comment by Frederick Kellison-Linn (JIRA) It looks to me like this happens during constraint simplification when the entire key path literal can be resolved before we have the necessary external context to determine whether the literal should be a function or a key path. In such a situation we assume that it will resolve to a key path... but in a situation like `.self` we can trivially resolve the keypath during constraint generation and so we never even have a chance to get the function type context. |
Additional Detail from JIRA
md5: 51619b34af87072237ed5a533887a7df
cloned to:
Issue Description:
The proposal which was Implemented in Swift 5.2 for Key Path Expressions as Functions has examples which do not compile. https://github.com/apple/swift-evolution/blob/master/proposals/0249-key-path-literal-function-expressions.md
When run in a playground:
[Edited to remove symptoms of a separate bug tracked as SR-12991.]
My biggest issue is the inability to use `(.self)` as a lot of my uses of `compactMap` involve filtering nil values in the array and not based on a property.
The text was updated successfully, but these errors were encountered: