Swift: switch/case destructuring pattern matching for arbitrary types

Originator:rix.rob
Number:rdar://17501298 Date Originated:29-Jun-2014 11:06 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode6-Beta2 (6A216f)
Classification:Enhancement Reproducible:Always
 
Summary:
switch/case could use a pattern matching protocol to provide destructuring pattern matching for arbitrary types. For example:

protocol PatternMatchable {
	typealias Pattern
	func destructure() -> Pattern
}

struct PatternMatchableThing {
	let name: String
	let homeworld: String
	
	func destructure() -> Pattern {
		return (name, homeworld)
	}
}

switch patternMatchableThing {
case PatternMatchableThing(name, homeworld):
	println("\(name) comes from \(homeworld)")
}


Steps to Reproduce:
N/A


Expected Results:
N/A


Actual Results:
N/A


Regression:
N/A


Notes:
I’m attempting to workaround this with the above-defined protocol and explicitly calling it; I don’t get nice type-matching syntax, but I’m hopeful that it can be made to work.

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!