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-6905] Dictionary(uniqueKeysWithValues:) does not accept sequence of labeled tuples #49454

Closed
martinr448 opened this issue Feb 2, 2018 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@martinr448
Copy link

Previous ID SR-6905
Radar None
Original Reporter @martinr448
Type Bug
Status Resolved
Resolution Duplicate
Environment

Tested with Xcode 9.2 (9C40b) and 9.3 beta (9Q98q).

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

md5: b490eff86091f31e423165d5d7a660f6

duplicates:

  • SR-6305 Cannot initialise Dictionary from an EnumeratedSequence

Issue Description:

(From https://forums.swift.org/t/creating-a-dictionary-from-an-enumeratedsequence/9472 )

The following code does not compile:

let array = ["zero", "one", "two", "three"]
let dict = Dictionary(uniqueKeysWithValues: array.enumerated())
// error: Generic parameter 'Key' could not be inferred

The same happens with the other dictionary initializer taking a key/value sequence:

let array = ["zero", "one", "two", "three"]
let dict = Dictionary(array.enumerated(), uniquingKeysWith: { $1 })
// error: Generic parameter 'Key' could not be inferred

The reason seems to be that the tuples from the EnumeratedSequence have labels, in this case (offset: Int, element: String). Another example which produces the same error:

typealias Pair = (a: Int, b: String)

let seq = AnySequence<Pair>([(1, "one")])
let dict = Dictionary(uniqueKeysWithValues: seq)
// Generic parameter 'Key' could not be inferred

Removing the tuple labels makes it compile and work as expected:

typealias Pair = (Int, String)

let seq = AnySequence<Pair>([(1, "one")])
let dict = Dictionary(uniqueKeysWithValues: seq)
print(dict) // [1: "one"]
@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

1 participant