[swift-users] Clarification on the role of `CustomStringConvertible`

Daniel Dunbar daniel_dunbar at apple.com
Wed Jun 29 15:33:05 CDT 2016


I would like some clarification regarding the expected role of `CustomStringConvertible` for types which have a natural, lossless, string representation.

I don't have good terminology to use here, but is the expectation that the "textual string representation" is a readable description of the object? This seems to correspond to the synthesized (?) default definition for struct types, as well as some of the uses for Foundation types without natural string representations (i.e. `Foundation.Notification` reports something like "name = foo, object = ..., userInfo = ...").

Or, is the expectation that it provide a string *representation* of the object, for objects where that makes sense. This corresponds to the use in `Foundation.UUID`, or the example of `Point.description` from the stdlib's `CustomStringConvertible` documentation.

Another way of phrasing this question is: for types which have a reversible string representation, should I expect `CustomStringConvertible.description` to give me a string I could use to reconstruct an instance _without_ knowing its type?

And another way (my actual question) is, given a `struct Path` object which contains a path string, what should `print(Path("a"))` print?

 - Daniel



More information about the swift-users mailing list