Allow bridging "as" patterns in switch cases <rdar://problem/17408934>.

We now allow switches like this:

  switch anyObject {
  case let str as String: // bridged via NSString
    println(str) 

  case let intArr as [Int]: // bridged via NSArray
    println(intArr)

  default:
  }

Note that we do not allow collection downcasting in switch statements
(yet); that's covered by <rdar://problem/17897378>.



Swift SVN r20976
This commit is contained in:
Doug Gregor
2014-08-03 19:58:49 +00:00
parent c25eac69c2
commit 1c484d91a0
2 changed files with 59 additions and 11 deletions

View File

@@ -116,3 +116,62 @@ testNSMutableStringMatch("foobar")
// CHECK: nomatch
testNSMutableStringMatch("nope")
func testAnyObjectDowncast(obj: AnyObject) {
switch obj {
case let str as String:
println("String: \(str)")
case let int as Int:
println("Int: \(int)")
case let nsStrArr as [NSString]:
println("NSString array: \(nsStrArr)")
case let intArr as [Int]:
println("Int array: \(intArr)")
case let dict as Dictionary<String, Int>:
println("Dictionary<String, Int>: \(dict)")
default:
println("Did not match")
}
}
// CHECK: String: hello
testAnyObjectDowncast("hello")
// CHECK: Int: 5
testAnyObjectDowncast(5)
// CHECK: NSString array: [hello, swift, world]
testAnyObjectDowncast(["hello", "swift", "world"] as NSArray)
// CHECK: Int array: [1, 2, 3, 4, 5]
testAnyObjectDowncast([1, 2, 3, 4, 5])
// CHECK: Dictionary<String, Int>: [hello: 1, world: 2]
testAnyObjectDowncast(["hello" : 1, "world" : 2])
func testNSArrayDowncast(nsArr: NSArray) {
switch nsArr {
case let strArr as [String]:
println("[String]: \(strArr)")
case let intArr as [Int]:
println("[Int]: \(intArr)")
default:
println("Did not match");
}
}
// CHECK: [String]: [a, b, c]
testNSArrayDowncast(["a", "b", "c"])
// CHECK: [Int]: [1, 2, 3]
testNSArrayDowncast([1, 2, 3])
// CHECK: Did not match
testNSArrayDowncast([[1, 2], [3, 4], [5, 6]])