[swift-users] Why do collections use the debugDescription on their elements?

Martin R martinr448 at gmail.com
Thu Jan 5 09:46:38 CST 2017


I wonder why the description method of collections use the debugDescription method to print their elements, and not the description method. Example:

    let s = "a\"b"
    print(s) // a"b
    print(s.debugDescription) // "a\"b"

    let x = 1.1
    print(x) // 1.1
    print(x.debugDescription) // 1.1000000000000001

    let d = [ s : x ]
    print(d) // ["a\"b": 1.1000000000000001]


- The embedded quotation mark is printed as " in print(s), but as \" in print(d).
- The floating point number is printed with a higher precision in the dictionary description.

The same happens with `Array`, `Set` and also `Optional`. Is there a special reason? It seems unnecessary and a possible cause for confusion to me.

Regards,
Martin



More information about the swift-users mailing list