Skip to content

Commit 4c41c12

Browse files
committed
[stdlib] Introduced the CustomPlaygroundDisplayConvertible protocol.
This protocol is intended as a replacement for `CustomPlaygroundQuickLookable` and `PlaygroundQuickLook`. It allows a type to return a substitute instance for use during playground logging.
1 parent d4c013c commit 4c41c12

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ set(SWIFTLIB_SOURCES
170170
CollectionOfOne.swift
171171
ExistentialCollection.swift.gyb
172172
Mirror.swift
173+
PlaygroundDisplay.swift
173174
CommandLine.swift
174175
SliceBuffer.swift
175176
Tuple.swift.gyb

stdlib/public/core/GroupInfo.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@
152152
"CompilerProtocols.swift",
153153
"ShadowProtocols.swift"
154154
],
155+
"Playground": [
156+
"PlaygroundDisplay.swift"
157+
],
155158
"Misc": [
156159
"AnyHashable.swift",
157160
"Interval.swift",
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===--- PlaygroundDisplay.swift ------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
/// A type that supplies a custom description for playground logging.
14+
///
15+
/// All types have a default description for playgrounds. This protocol
16+
/// allows types to provide custom descriptions which are then logged in
17+
/// place of the original instance.
18+
///
19+
/// Playground logging can generate, at a minimum, a structured description
20+
/// of any type. Playground logging is also capable of generating a richer,
21+
/// more specialized description of core types -- for instance, the contents
22+
/// of a `String` are logged, as are the components of an `NSColor` or
23+
/// `UIColor`.
24+
///
25+
/// The current playground logging implementation logs specialized
26+
/// descriptions of at least the following types:
27+
///
28+
/// - `String` and `NSString`
29+
/// - `Int` and `UInt` (including the sized variants)
30+
/// - `Float` and `Double`
31+
/// - `Bool`
32+
/// - `Date` and `NSDate`
33+
/// - `NSAttributedString`
34+
/// - `NSNumber`
35+
/// - `NSRange`
36+
/// - `URL` and `NSURL`
37+
/// - `CGPoint`, `CGSize`, and `CGRect`
38+
/// - `NSColor`, `UIColor`, `CGColor`, and `CIColor`
39+
/// - `NSImage`, `UIImage`, `CGImage`, and `CIImage`
40+
/// - `NSBezierPath` and `UIBezierPath`
41+
/// - `NSView` and `UIView`
42+
///
43+
/// Playground logging may also be able to support specialized descriptions
44+
/// of other types.
45+
///
46+
/// Implementors of `CustomPlaygroundDisplayConvertible` may return a value of
47+
/// one of the above types to also receive a specialized log description.
48+
/// Implementors may also return any other type, and playground logging will
49+
/// generated structured logging for the returned value.
50+
///
51+
/// - note: `CustomPlaygroundDisplayConvertible` conformances chain -- that is,
52+
/// if `playgroundDescription` returns an instance which itself conforms to
53+
/// `CustomPlaygroundDisplayConvertible`, then playground logging will ask for
54+
/// that instance's `playgroundDescription` and so on. It is permissible for
55+
/// playground logging implementations to place a reasonable limit on this
56+
/// kind of chaining to prevent infinite loops.
57+
public protocol CustomPlaygroundDisplayConvertible {
58+
/// Returns the custom playground description for this instance.
59+
///
60+
/// If this type has value semantics, the instance returned should be
61+
/// unaffected by subsequent mutations if possible.
62+
var playgroundDescription: Any { get }
63+
}

0 commit comments

Comments
 (0)