{"id":22662,"date":"2018-09-06T15:45:02","date_gmt":"2018-09-06T19:45:02","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=22662"},"modified":"2018-09-06T15:45:02","modified_gmt":"2018-09-06T19:45:02","slug":"why-youre-not-supposed-to-call-swifts-description","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/09\/06\/why-youre-not-supposed-to-call-swifts-description\/","title":{"rendered":"Why You&rsquo;re Not Supposed to Call Swift&rsquo;s description"},"content":{"rendered":"<p><a href=\"https:\/\/oleb.net\/2018\/dont-call-description\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/2018\/dont-call-description\/\">\n<p>However, you should absolutely not use the following pattern, where a function argument is constrained to <code>Custom[Debug]StringConvertible<\/code>, either with a generic constraint or a plain type specification[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>Instead, the function should accept any type, because anything is printable:<\/p>\n<pre>func doSomething3&lt;T&gt;(with x: T) {\n    \/\/ ...\n    \/\/ Call String(describing: x)\n}\n<\/pre>\n<p>[&#8230;]<\/p>\n<p>When you rely on <code>LosslessStringConvertible<\/code> semantics, you <em>should<\/em> absolutely access the <code>description<\/code> property directly, despite the above advice to the contrary. As we have seen, the alternative <code>String(describing:)<\/code> prefers <code>Text&#x200B;Output&#x200B;Streamable<\/code>&rsquo;s representation over <code>description<\/code> if it&rsquo;s available, and you can&rsquo;t be 100&#x202F;% certain that representation is identical to the value&rsquo;s <code>description<\/code>, however unlikely any differences are.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Ole Begemann: However, you should absolutely not use the following pattern, where a function argument is constrained to Custom[Debug]StringConvertible, either with a generic constraint or a plain type specification[&#8230;] [&#8230;] Instead, the function should accept any type, because anything is printable: func doSomething3&lt;T&gt;(with x: T) { \/\/ ... \/\/ Call String(describing: x) } [&#8230;] When [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2018-09-06T19:45:04Z","apple_news_api_id":"dac79c06-3ea6-45e5-99b9-ae112db35f05","apple_news_api_modified_at":"2018-09-06T19:45:05Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/A2secBj6mReWZua4RLbNfBQ","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[46,71,901],"class_list":["post-22662","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/22662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=22662"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/22662\/revisions"}],"predecessor-version":[{"id":22663,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/22662\/revisions\/22663"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=22662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=22662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=22662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}