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-8635] "reduce(into:)" Cannot subscript a value of incorrect or ambiguous type #3642

Closed
swift-ci opened this issue Aug 24, 2018 · 1 comment

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-8635
Radar None
Original Reporter candy_appl3 (JIRA User)
Type Bug
Status Resolved
Resolution Invalid
Environment

Model Name: MacBook Pro

Model Identifier: MacBookPro11,2

Processor Name: Intel Core i7

Processor Speed: 2 GHz

Number of Processors: 1

Total Number of Cores: 4

L2 Cache (per Core): 256 KB

L3 Cache: 6 MB

Memory: 8 GB

Boot ROM Version: MBP112.0146.B00

SMC Version (system): 2.18f15

Additional Detail from JIRA
Votes 0
Component/s Compiler, Foundation, Xcode Playground Support
Labels Bug
Assignee None
Priority Medium

md5: 310fe1d84b7eae7af46159b65104f21c

Issue Description:

let foo: [String: Int] = "foo; foo; bar; bar; bar bar; foo foo"
.components(separatedBy: "; ")
.reduce(into: [String: Int]()) { 
    $0[$1] = $0[$1] == nil ? 1 : $0[$1]! += 1 
}

Hey. I want with a minimal amount of code, actively using syntactic sugar and in one call chain to calculate the frequency of repeating lines in the source line.
With the ternary operator this approach does not work, the compiler swears: "Cannot subscript a value of incorrect or ambiguous type".

But using the usual "if let" statement everything works in a distorted way.
Tell me, where did I make a mistake?

@belkadan
Copy link

+= doesn't produce a value in Swift, i.e. you can't add to a variable and produce it at the same time. However, there is a better way to get what you want anyway:

$0[$1, default: 0] += 1

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
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

2 participants