mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Mock up a naive Printable protocol, and do some dirty tricks in the runtime to implement a 'printAny' function that uses swift_conformsToProtocol to look up a conformance to Printable if the type has one, or falls back to a dumb opaque printing if it doesn't. Use this to make Array<T> Printable in some way or another for all T. Swift SVN r13902
29 lines
555 B
Swift
29 lines
555 B
Swift
// RUN: %target-run-simple-swift | FileCheck %s
|
||
|
||
// CHECK: [1, 2, 3]
|
||
[1, 2, 3].printSelf()
|
||
println()
|
||
|
||
// CHECK: [foo, bar, bas]
|
||
["foo", "bar", "bas"].printSelf()
|
||
println()
|
||
|
||
struct Unprintable {
|
||
val x: Int
|
||
}
|
||
|
||
// CHECK: [<something>, <something>, <something>]
|
||
[Unprintable(1), Unprintable(2), Unprintable(3)].printSelf()
|
||
println()
|
||
|
||
struct CanBePrinted : Printable {
|
||
val x: Int
|
||
func printSelf() {
|
||
print("►\(x)◀︎")
|
||
}
|
||
}
|
||
|
||
// CHECK: [►1◀︎, ►2◀︎, ►3◀︎]
|
||
[CanBePrinted(1), CanBePrinted(2), CanBePrinted(3)].printSelf()
|
||
println()
|