Files
swift-mirror/test/Interpreter/SDK/CoreFoundation_casting.swift
Chris Willmore 03a6190a1f <rdar://problem/19031957> Change failable casts from "as" to "as!"
Previously the "as" keyword could either represent coercion or or forced
downcasting. This change separates the two notions. "as" now only means
type conversion, while the new "as!" operator is used to perform forced
downcasting. If a program uses "as" where "as!" is called for, we emit a
diagnostic and fixit.

Internally, this change removes the UnresolvedCheckedCastExpr class, in
favor of directly instantiating CoerceExpr when parsing the "as"
operator, and ForcedCheckedCastExpr when parsing the "as!" operator.

Swift SVN r24253
2015-01-08 00:33:59 +00:00

85 lines
1.9 KiB
Swift

// RUN: %target-run-simple-swift | FileCheck %s
import CoreFoundation
import Foundation
class SwiftClass { }
func genericCast<T>(x: AnyObject, _: T.Type) -> T? {
return x as? T
}
func genericCastUnconditional<T>(x: AnyObject, _: T.Type) -> T {
return x as! T
}
// Check _cfTypeID() on a Swift class
let nsObject = NSObject()
let swiftObject = SwiftClass()
assert(CFGetTypeID(nsObject) == CFGetTypeID(swiftObject))
// Check CFString <-> AnyObject
func testCFStringAnyObject() {
// Create a CFString
let cfStr: CFString
= CFStringCreateWithCString(nil, "Swift", CFStringBuiltInEncodings.ASCII.rawValue)
// CHECK: Swift
println(cfStr)
// Convert it to AnyObject
let anyObject: AnyObject = cfStr
// CHECK: Swift
println(anyObject)
// Convert it back to a CFString
let cfStr2 = anyObject as! CFString
// CHECK: Swift
println(cfStr2)
// Conditional cast through a generic to a CFString
if let cfStr3 = genericCast(anyObject, CFString.self) {
// CHECK: Swift
println(cfStr3)
} else {
println("Conditional cast failed")
}
// Forced cast through a generic to a CFString
let cfStr4 = genericCastUnconditional(anyObject, CFString.self)
// CHECK: Swift
println(cfStr4)
// CHECK: done
println("done")
}
testCFStringAnyObject()
// Check CFString.Type <-> AnyObject.Type
func testCFStringAnyObjectType() {
let cfStr: CFString
= CFStringCreateWithCString(nil, "Swift", CFStringBuiltInEncodings.ASCII.rawValue)
let cfStrType = cfStr.dynamicType
// CHECK: [[STRING_CLASS:(NS|CF).*String]]
println(cfStrType)
// Convert to AnyObject.Type
let anyObjectType: AnyObject.Type = cfStrType
// CHECK: [[STRING_CLASS]]
println(anyObjectType)
// Convert back to CFString.Type
let cfStrType2 = anyObjectType as! CFString.Type
// CHECK: [[STRING_CLASS]]
println(cfStrType2)
// CHECK: done
println("done")
}
testCFStringAnyObjectType()