Skip to content

fix(PlaygroundLogger): use String(describing:) for CustomStringConvertible in generateSummary to remove extra debug quotes#82

Open
AkshatRaj00 wants to merge 1 commit into
apple:mainfrom
AkshatRaj00:fix/playground-logger-string-reflecting
Open

fix(PlaygroundLogger): use String(describing:) for CustomStringConvertible in generateSummary to remove extra debug quotes#82
AkshatRaj00 wants to merge 1 commit into
apple:mainfrom
AkshatRaj00:fix/playground-logger-string-reflecting

Conversation

@AkshatRaj00

Copy link
Copy Markdown

Summary

This PR fixes a display bug in PlaygroundLogger where the Playground sidebar shows unwanted surrounding quotes for user-defined CustomStringConvertible types.

File: PlaygroundLogger/PlaygroundLogger/LogEntry+Reflection.swift
Related Issue: #81


The Bug

generateSummary() used String(reflecting:) for both CustomStringConvertible and CustomDebugStringConvertible:

// BEFORE (buggy)
if instance is CustomStringConvertible || instance is CustomDebugStringConvertible {
    return String(reflecting: instance)
}

String(reflecting:) is designed for debug output — it calls debugDescription or CustomReflectable, and adds surrounding quotes for String values and similar types. When a user implements CustomStringConvertible expecting their .description to appear in the sidebar, they instead see their output wrapped in extra quotes:

struct Greeting: CustomStringConvertible {
    var description: String { return "Hello, Playground!" }
}
let g = Greeting()
// Sidebar shows: "Hello, Playground!"  ← extra quotes from String(reflecting:)
// Expected:      Hello, Playground!

The Fix

Split the check into two separate branches:

// AFTER (fixed)
// Use String(describing:) for CustomStringConvertible — calls .description directly,
// no debug decoration added.
if instance is CustomStringConvertible {
    return String(describing: instance)
}

// Only use String(reflecting:) for types that are *solely* CustomDebugStringConvertible.
if instance is CustomDebugStringConvertible {
    return String(reflecting: instance)
}

String(describing:) calls .description directly without any wrapping — exactly what the Playground sidebar should show for user-defined types.


Before / After

Type Before After
struct implementing CustomStringConvertible "Hello, Playground!" (extra quotes) Hello, Playground!
class implementing CustomStringConvertible "MyClass description" (extra quotes) MyClass description
Type implementing only CustomDebugStringConvertible Same behavior Same behavior ✅
Plain String (handled by the earlier as? String branch) Unchanged Unchanged ✅

Scope of Change

  • 1 file changed: PlaygroundLogger/PlaygroundLogger/LogEntry+Reflection.swift
  • 2 lines changed in generateSummary() — one || condition split into two separate if blocks
  • No API changes, no behaviour changes for types not conforming to either protocol

Reported and fixed as part of open-source contribution.

…ummary

Previously, generateSummary() used String(reflecting:) for any type
conforming to CustomStringConvertible or CustomDebugStringConvertible.
String(reflecting:) adds debug decoration (e.g. surrounding quotes for
String values and other types), causing the Playground sidebar to show
"\"Hello\"" instead of "Hello" for user-defined CustomStringConvertible types.

Fix: Use String(describing:) for CustomStringConvertible conformers (which
calls .description directly without decoration), and keep String(reflecting:)
only for types that are solely CustomDebugStringConvertible.

Fixes: apple#81
@AkshatRaj00 AkshatRaj00 requested a review from cwakamo as a code owner May 23, 2026 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant