{"id":17379,"date":"2017-03-10T13:46:52","date_gmt":"2017-03-10T18:46:52","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=17379"},"modified":"2017-03-10T13:46:52","modified_gmt":"2017-03-10T18:46:52","slug":"playing-jenga-with-swifts-type-checker","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/03\/10\/playing-jenga-with-swifts-type-checker\/","title":{"rendered":"Playing Jenga With Swift&rsquo;s Type Checker"},"content":{"rendered":"<p><a href=\"https:\/\/blog.definiteloops.com\/playing-jenga-with-swifts-type-checker-7c4f13f6d2a4\">Vincent Esche<\/a> (via <a href=\"https:\/\/twitter.com\/iTod\/status\/836245866478354432\">Todd Ditchendorf<\/a>):<\/p>\n<blockquote cite=\"https:\/\/blog.definiteloops.com\/playing-jenga-with-swifts-type-checker-7c4f13f6d2a4\"><p>One would expect both of these minimalistic programs to be <em>semantically equivalent<\/em> and thus be handled identically by the compiler, no?<\/p><p>The answer to this question and many more can be found in the Swift project&rsquo;s official documentation on its <a href=\"https:\/\/github.com\/apple\/swift\/blob\/master\/docs\/TypeChecker.rst\">&ldquo;Type Checker Design and Implementation&rdquo;<\/a> (emphasis mine):<\/p><blockquote cite=\"https:\/\/github.com\/apple\/swift\/blob\/master\/docs\/TypeChecker.rst\"><p>Swift implements <em>bi-directional type inference<\/em> using a <em>constraint-based type checker<\/em> that is reminiscent of the classical <em>Hindley-Milner type inference algorithm<\/em>. [&#8230;]<\/p>\n<p>The Swift language contains a number of features not part of the Hindley-Milner type system, including constrained <em>polymorphic types<\/em> and <em>function overloading<\/em>, which <em>complicate the presentation and implementation<\/em> somewhat. On the other hand, Swift <em>limits the scope of type inference to a single expression or statement<\/em>, for purely practical reasons: we expect that we can provide better performance and vastly better diagnostics when the problem is limited in scope.<\/p><\/blockquote>\n<p>[&#8230;]<\/p>\n<p>Repeat after me: &ldquo;Swift limits the scope of type inference to a <em>single expression<\/em> or <em>statement<\/em>&rdquo;.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Vincent Esche (via Todd Ditchendorf): One would expect both of these minimalistic programs to be semantically equivalent and thus be handled identically by the compiler, no?The answer to this question and many more can be found in the Swift project&rsquo;s official documentation on its &ldquo;Type Checker Design and Implementation&rdquo; (emphasis mine):Swift implements bi-directional type inference [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","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":[46,71,901],"class_list":["post-17379","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\/17379","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=17379"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17379\/revisions"}],"predecessor-version":[{"id":17380,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17379\/revisions\/17380"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=17379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=17379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=17379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}