Skip to content
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-13347] compactMap has different semantics with closure than keyPath. #55787

Open
nicklockwood opened this issue Aug 4, 2020 · 0 comments
Open

Comments

@nicklockwood
Copy link
Contributor

@nicklockwood nicklockwood commented Aug 4, 2020

Previous ID SR-13347
Radar None
Original Reporter @nicklockwood
Type Bug
Environment

Xcode 11.6, Swift 5.2

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 0300b2587bde2e6fea9e7b74b21a2d20

Issue Description:

The following code compiles without a warning, even though the `compactMap()` is arguably a mistake and should be replaced by `map()`:

struct Item {
    var text: String
}

let items = [Item(text: "foo")]

let filteredItems = items.compactMap { $0.text }

But if I replace the last line with:

let filteredItems = items.compactMap(\.text)

It fails with:

Generic parameter 'ElementOfResult' could not be inferred
Key path value type 'String' cannot be converted to contextual type 'ElementOfResult?'

I'm not sure whether the better fix here would be for `compactMap { $0.text }` to produce a warning, or for `compactMap(.text)` to silently succeed, but it definitely seems wrong that these aren't consistent.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant