-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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-4745] for (index, (a, b)) in dict.enumerated() produces an error #47322
Comments
Another case of inconsistent tuple conversions. cc @slavapestov, @rudkx |
It looks like Swift 4 doesn't allow even the simple case of matching count/element on an enumerated array—is that the intended behavior? let a = [1, 2, 3, 4, 5]
let b = a.enumerated().map { (count, element) in "\(count): \(element)" }
// The error message is also unhelpful:
// error: cannot convert value of type 'String' to closure result type '_' |
Yes, that one's correct. Closure arguments are not pattern matches. @xedin is working on improving this particular issue. |
@natecook1000, it seems like what you describe is even stricter than what's reported in SR-4738? |
@natecook1000 Your example is related to SR-4738 and it's going to produce following diagnostic:
But `for` loop diagnostics might require some more work though. |
for-loops really are pattern matches and do support destructuring. Our handling of tuple labels is just inconsistent enough that some of these cases work and some don't. |
@belkadan, wait, what?! Are you saying that the original example here is supposed to work? |
One of these is assignment, like To be fair, I'm not against adding destructuring-like capabilities to closures, but that would be a language change, and would have to go through the Swift Evolution Process. |
@belkadan, what would adding support for destructuring tuples in closure arguments entail in the compiler? Is that completely new feature, or it could reuse what's in for-each? It looks like Reitzig (JIRA User) was suggesting the same thing in comments to SR-2008… maybe we should jointly draft an SE proposal? |
It's 80% a new feature. The compiler would have to figure out what the proper type of the closure is anyway (the hard part) and then assign from that type into local variables for the destructured parameters (the easy part). Please do take it to swift-evolution if you're interested! |
Any thoughts on requiring a |
Attachment: Download
Environment
Apple Swift version 3.1 (swiftlang-802.0.51 clang-802.0.41)
Target: x86_64-apple-macosx10.9
Additional Detail from JIRA
md5: 3cf8b249d7223d158ce1e39c4c03487e
duplicates:
relates to:
Issue Description:
produces
The text was updated successfully, but these errors were encountered: