Files
swift-mirror/test/stdlib/Print.swift
Joe Groff 3fded63cac stdlib: Proof-of-concept 'print' implementation for an arbitrary Array.
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
2014-02-14 05:15:33 +00:00

29 lines
555 B
Swift
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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()