Swift arrays should be able to be pattern matched against and deconstructed like tuples

Originator:injahta
Number:rdar://17223702 Date Originated:06/08/2014
Status:Open Resolved:
Product:Swift Product Version:
Classification:Enhancement Reproducible:
 
Summary:
Pattern matching in Swift is quite powerful, and arrays can be used in a pattern, but arrays cannot be deconstructed and bound to variables in patterns.

It would be useful if arrays could be matched against and deconstructed in the same way tuples are. Something like this:

switch array {
 case let [x]: ///An array with a single element
 case let [x:xs]: /// An array with a head followed by a tail
}

Steps to Reproduce:
1. Use an array in a switch statement
2. Try to bind variable names in a pattern in the same way as tuples.

Expected Results:
Arrays can be deconstructed and matched against just like tuples can

Actual Results:
Arrays cannot be deconstructed and bound to variables like tuples can.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!