{"id":14471,"date":"2016-05-09T12:08:28","date_gmt":"2016-05-09T16:08:28","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=14471"},"modified":"2016-05-09T19:28:22","modified_gmt":"2016-05-09T23:28:22","slug":"demangling-swift-with-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/05\/09\/demangling-swift-with-swift\/","title":{"rendered":"Demangling Swift With Swift"},"content":{"rendered":"<p><a href=\"http:\/\/www.cocoawithlove.com\/blog\/2016\/05\/01\/swift-name-demangling.html\">Matt Gallagher<\/a> (<a href=\"https:\/\/twitter.com\/cocoawithlove\/status\/728921740068282369\">tweet<\/a>):<\/p>\n<blockquote cite=\"http:\/\/www.cocoawithlove.com\/blog\/2016\/05\/01\/swift-name-demangling.html\"><p>I rewrote the C++ implementation of Swift&rsquo;s <a href=\"https:\/\/github.com\/apple\/swift\/blob\/master\/lib\/Basic\/Demangle.cpp\">Demangle.cpp<\/a> in Swift. <a href=\"https:\/\/github.com\/mattgallagher\/CwlDemangle\">My reimplementation<\/a> is completely standalone and can be dropped directly into any Swift project.<\/p>\n<p>[&#8230;]<\/p>\n<p>From the first line, we can see a difference between the Swift and C++ versions. Swift has a standard set of protocols and constraints for defining data providers, so it makes sense to use them. C++ <em>could<\/em> use a template parameter to define the data provider but since C++ lacks an equivalent to Swift&rsquo;s protocol constraints and lacks a corresponding set of standard behaviors, the mulitple constraints for the data provider would be a confusing black box thrust upon any user of <code>NameSource<\/code> &#8211; likely manifesting in weird errors in internal headers if any requirements were not met.<\/p>\n<p>[&#8230;]<\/p>\n<p>But the most visible difference in adopting Swift error handling is a significant reduction in code size. Switching to Swift error handling immediately eliminated 149 <code>return nullptr<\/code> early exit lines from the C++ version. Furthermore, Swift can happily exit from a function in the middle of an expression when a parse attempt fails instead of needing to break expressions into multiple pieces either side of early exits.<\/p>\n<p>[&#8230;]<\/p>\n<p>Many large C++ projects &#8211; Swift included &#8211; are compiled with C++ exceptions entirely disabled. Why deliberately remove a potentially useful feature from the language? The Swift developers answer this question when <a href=\"https:\/\/github.com\/apple\/swift\/blob\/master\/docs\/ErrorHandlingRationale.rst#id1\">considering error handling options for Swift<\/a>[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>This shows the advantage of a two value <code>switch<\/code> being idiomatic in Swift: compared to the strange series of <code>#define<\/code>, <code>if<\/code> and <code>switch<\/code> constructions in C++, this is clear, simple and readable.<\/p><\/blockquote>\n<p>He downplays the usefulness of the project, focusing on the code comparison with C++. However, I think the code is actually very useful because I want to record readable <a href=\"http:\/\/mjtsai.com\/blog\/2015\/08\/27\/capturing-swift-error-context\/\">stack traces<\/a> when throwing or propagating an error.<\/p>\n<p>Update (2016-05-09): <a href=\"https:\/\/twitter.com\/0xced\/status\/729753461810696192\">C&eacute;dric Luthi<\/a> mentions the built-in <code>_stdlib_demangleName()<\/code>, but I think it&rsquo;s private API.<\/p>","protected":false},"excerpt":{"rendered":"<p>Matt Gallagher (tweet): I rewrote the C++ implementation of Swift&rsquo;s Demangle.cpp in Swift. My reimplementation is completely standalone and can be dropped directly into any Swift project. [&#8230;] From the first line, we can see a difference between the Swift and C++ versions. Swift has a standard set of protocols and constraints for defining data [&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":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","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":[326,79,46,857,74,270,71,901],"class_list":["post-14471","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-c-plus-plus","tag-exceptions","tag-languagedesign","tag-nserror","tag-opensource","tag-parser","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14471","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=14471"}],"version-history":[{"count":4,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14471\/revisions"}],"predecessor-version":[{"id":14482,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14471\/revisions\/14482"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=14471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=14471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=14471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}