{"id":37365,"date":"2022-10-17T16:22:05","date_gmt":"2022-10-17T20:22:05","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=37365"},"modified":"2022-10-17T16:23:51","modified_gmt":"2022-10-17T20:23:51","slug":"a-possible-vision-for-macros-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/10\/17\/a-possible-vision-for-macros-in-swift\/","title":{"rendered":"A Possible Vision for Macros in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/forums.swift.org\/t\/a-possible-vision-for-macros-in-swift\/60900\">Douglas Gregor<\/a> (via <a href=\"https:\/\/twitter.com\/jckarter\/status\/1582088298624823297\">Joe Groff<\/a>):<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/a-possible-vision-for-macros-in-swift\/60900\"><p>Macros can offer a way out of this conundrum, because they allow one to introduce boilerplate-reducing facilities without requiring a bespoke language extension or a separate source-manipulating tool to do so. A good macro system could replace the need for a swath of new language features, but one must be careful not to degrade the development experience by making it too hard to build good tools (i.e., C macros are notorious for breaking tooling).<\/p>\n<p>We propose to introduce a macro system into Swift. At a very high level, macros can be explicitly triggered with syntax such as <code>#stringify(x + y)<\/code>, subsuming a number of existing expressions that use similar syntax already (<code>#line<\/code>, <code>#colorLiteral(...)<\/code>, etc.) with a general features. Macros can also be triggered more implicitly in response to type checking, e.g., applying a macro to the argument passed to a function. Macro arguments are type-checked so that tooling behaves similarly to today, but macro evaluation involves transforming the syntax so that it can serve a number of different use cases.<\/p>\n<p><a href=\"https:\/\/gist.github.com\/DougGregor\/4f3ba5f4eadac474ae62eae836328b71\">A possible vision for macros in Swift<\/a> talks about the design space and proposes a path for macros in Swift.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2018\/07\/17\/swift-gyb\/\">Swift GYB<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2016\/12\/12\/insanity-template-based-code-generation-for-swift\/\">Sourcery: Template-Based Code Generation for Swift<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/12\/09\/swift-plans\/\">Swift Plans<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/01\/30\/swift-no-macros-no-compromises\/\">Swift: No Macros, No Compromises<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Douglas Gregor (via Joe Groff): Macros can offer a way out of this conundrum, because they allow one to introduce boilerplate-reducing facilities without requiring a bespoke language extension or a separate source-manipulating tool to do so. A good macro system could replace the need for a swath of new language features, but one must be [&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":"2022-10-17T20:22:08Z","apple_news_api_id":"f430af46-8253-4467-bf46-8c2e90f26a3d","apple_news_api_modified_at":"2022-10-17T20:23:54Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/A9DCvRoJTRGe_RowukPJqPQ","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,941,71,901],"class_list":["post-37365","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-macros","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/37365","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=37365"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/37365\/revisions"}],"predecessor-version":[{"id":37367,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/37365\/revisions\/37367"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=37365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=37365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=37365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}