Files
swift-mirror/test/Interpreter/optional.swift
John McCall 53565efe8d Teach the type-checker to bind and capture optional
values when casting to or from optional types.

rdar://16076966

Swift SVN r15211
2014-03-18 22:57:36 +00:00

74 lines
2.4 KiB
Swift

// RUN: %target-run-simple-swift | FileCheck %s
class A {
func printA() { print("A") }
}
class B : A {
@override func printA() { print("B") }
}
func print(v: A) { v.printA() }
func printOpt<T>(subprint: T->())(x: T?) {
switch (x) {
case .Some(let y): print(".Some("); subprint(y); print(")")
case .None: print(".None")
}
}
func test(v: A????, cast: (A????) -> B?) {
printOpt(printOpt(printOpt(printOpt(print))))(v)
print(" as B: ")
printOpt(print)(cast(v))
print("\n")
}
test(.Some(.Some(.Some(.Some(A())))), { $0 as B })
test(.Some(.Some(.Some(.Some(B())))), { $0 as B })
test(.Some(.Some(.Some(.None))), { $0 as B })
test(.Some(.Some(.None)), { $0 as B })
test(.Some(.None), { $0 as B })
test(.None, { $0 as B })
// CHECK: .Some(.Some(.Some(.Some(A)))) as B: .None
// CHECK: .Some(.Some(.Some(.Some(B)))) as B: .Some(B)
// CHECK: .Some(.Some(.Some(.None))) as B: .None
// CHECK: .Some(.Some(.None)) as B: .None
// CHECK: .Some(.None) as B: .None
// CHECK: .None as B: .None
func test(v: A????, cast: (A????) -> B??) {
printOpt(printOpt(printOpt(printOpt(print))))(v)
print(" as B?: ")
printOpt(printOpt(print))(cast(v))
print("\n")
}
test(.Some(.Some(.Some(.Some(A())))), { $0 as B? })
test(.Some(.Some(.Some(.Some(B())))), { $0 as B? })
test(.Some(.Some(.Some(.None))), { $0 as B? })
test(.Some(.Some(.None)), { $0 as B? })
test(.Some(.None), { $0 as B? })
test(.None, { $0 as B? })
// CHECK: .Some(.Some(.Some(.Some(A)))) as B?: .None
// CHECK: .Some(.Some(.Some(.Some(B)))) as B?: .Some(.Some(B))
// CHECK: .Some(.Some(.Some(.None))) as B?: .Some(.None)
// CHECK: .Some(.Some(.None)) as B?: .None
// CHECK: .Some(.None) as B?: .None
// CHECK: .None as B?: .None
func test(v: A????, cast: (A????) -> B???) {
printOpt(printOpt(printOpt(printOpt(print))))(v)
print(" as B??: ")
printOpt(printOpt(printOpt(print)))(cast(v))
print("\n")
}
test(.Some(.Some(.Some(.Some(A())))), { $0 as B?? })
test(.Some(.Some(.Some(.Some(B())))), { $0 as B?? })
test(.Some(.Some(.Some(.None))), { $0 as B?? })
test(.Some(.Some(.None)), { $0 as B?? })
test(.Some(.None), { $0 as B?? })
test(.None, { $0 as B?? })
// CHECK: .Some(.Some(.Some(.Some(A)))) as B??: .None
// CHECK: .Some(.Some(.Some(.Some(B)))) as B??: .Some(.Some(.Some(B)))
// CHECK: .Some(.Some(.Some(.None))) as B??: .Some(.Some(.None))
// CHECK: .Some(.Some(.None)) as B??: .Some(.None)
// CHECK: .Some(.None) as B??: .None
// CHECK: .None as B??: .None