Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proposal for useful String.Index descriptions #2529

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lorentey
Copy link
Member

This retroactively conforms String.Index to CustomStringConvertible.

rdar://22794467

@rjmccall rjmccall added standard library addition Additive changes to the standard library LSG Contains topics under the domain of the Language Steering Group labels Jul 29, 2024
@benrimmington
Copy link
Contributor

You could mention that LLDB uses the same format (since Swift 5.8):


Was there any discussion about using debugDescription and CustomDebugStringConvertible instead? Or do you want to reserve those for a more verbose format?

@lorentey
Copy link
Member Author

lorentey commented Aug 1, 2024

Was there any discussion about using debugDescription and CustomDebugStringConvertible instead? Or do you want to reserve those for a more verbose format?

Yes! After pitching this stuff, I investigated the role of the CustomDebugStringConvertible protocol, and I've had a long-standing reminder to start a forum discussion clarifying it. Surprisingly, it turns out it debugDescription isn't actually there to produce a more verbose display, or to give more information about the item. (For all I know, it may have been designed for that purpose, but that is certainly not how Swift is using it.)

CustomDebugStringConvertible is somewhat of a misnomer -- it actual de facto purpose is to produce a display that is functionally equivalent to description, but in a form that works well in structured, syntactic output: i.e., the string it returns needs to be free of unpaired parenthesis/brackets/quotes/etc, naked top-level commas/spaces, etc.

Collection types such as Array or Dictionary use debugDescription to print their contents, to avoid unintelligible output. Therefore, debugDescription must not be overly verbose -- it just needs to produce a syntactically well-formed format. (It will appear in array/dictionary/etc descriptions, where brevity is important.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LSG Contains topics under the domain of the Language Steering Group standard library addition Additive changes to the standard library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants