-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Open
Labels
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfThe Swift compiler itselftype checkerArea → compiler: Semantic analysisArea → compiler: Semantic analysistype inferenceFeature: type inferenceFeature: type inference
Description
Previous ID | SR-16075 |
Radar | None |
Original Reporter | OMGHunter (JIRA User) |
Type | Bug |
Environment
Xcode 13.3 (13E113)
Additional Detail from JIRA
Votes | 0 |
Component/s | swift |
Labels | Bug |
Assignee | None |
Priority | Medium |
md5: 383ad533155d7b964df61e4e1f582e7d
Issue Description:
For the following code snippet, res1
and res2
surprisingly have different values:
func test() {
let a: String? = nil
let b: String? = nil
let foo: (String?) -> Int = { $0 == nil ? 0 : 1}
let res1 = [a, b].compactMap { $0 }.map(foo)
let res2 = [a, b].compactMap { $0 }.map { foo($0) }
print("res1: ", res1)
print("res2: ", res2)
}
test()
// res1: [0, 0]
// res2: []
According to the comment on StackOverflow (https://stackoverflow.com/a/71669873/4770353) it's a result of how the compiler selects the types of [a, b].compactMap
based on the downstream operation.
Metadata
Metadata
Assignees
Labels
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfThe Swift compiler itselftype checkerArea → compiler: Semantic analysisArea → compiler: Semantic analysistype inferenceFeature: type inferenceFeature: type inference