{"id":3146,"date":"2023-06-08T09:43:27","date_gmt":"2023-06-08T13:43:27","guid":{"rendered":"https:\/\/michaelrowe01.com\/?p=3146"},"modified":"2023-06-08T10:38:24","modified_gmt":"2023-06-08T14:38:24","slug":"expand-on-swift-macros","status":"publish","type":"post","link":"https:\/\/michaelrowe01.com\/index.php\/blog\/expand-on-swift-macros\/","title":{"rendered":"Expand on Swift Macros"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"660\" height=\"371\" data-attachment-id=\"3148\" data-permalink=\"https:\/\/michaelrowe01.com\/index.php\/blog\/expand-on-swift-macros\/attachment\/%ef%a3%bfwwdc23-17\/\" data-orig-file=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-17.png?fit=1920%2C1080&amp;ssl=1\" data-orig-size=\"1920,1080\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"\uf8ffWWDC23-17\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-17.png?fit=660%2C371&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-17.png?resize=660%2C371&#038;ssl=1\" alt=\"\" class=\"wp-image-3148\" srcset=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-17.png?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-17.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-17.png?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-17.png?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-17.png?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-17.png?w=1320&amp;ssl=1 1320w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Why Macros<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Lots of build in code expansion like Codable and property wrappers<\/li>\n\n\n\n<li>Used to allow your own language features, to eliminate tedium and boiler plate.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Design Philosophy<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Different from C Macros<\/li>\n\n\n\n<li>Distinctive use sites<\/li>\n\n\n\n<li>All start with either # or @ depending on type of macros<\/li>\n\n\n\n<li>Complete, type-checked, validated &#8211; macros must be all of this<\/li>\n\n\n\n<li>Inserted in a predictable additive ways.&nbsp; It can not delete anything<\/li>\n\n\n\n<li>Should not be&nbsp; Magic &#8211; you can expand the macro in line or step into it in the debugger.&nbsp; Even if it is a closed source library.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Translation Model<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Basic concept,&nbsp; extract the usage from code and sends to compiler plugin, in separate process and sandbox, and returns an expansion of code &#8211; which is added to your program &#8211; which is then compiled with your code.<\/li>\n\n\n\n<li>All macros have to be declared so that the compiler can do the prior step &#8211; this is pacifically the API for the macro.\u00a0 You can either import it via library\/framework or write it inline in your code.\n<ul class=\"wp-block-list\">\n<li>It must define it\u2019s role<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Macro Roles<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Role is a set of rules for the macro\n<ul class=\"wp-block-list\">\n<li>Where it can be used<\/li>\n\n\n\n<li>What types of code it expands into<\/li>\n\n\n\n<li>Where the expansions are inserted<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>They are responsible for achieving the goals.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"blob:https:\/\/michaelrowe01.com\/0fedb76b-5da5-4bf5-a3fe-708692d89c02\" alt=\"Screenshot 2023-06-07 at 17.29.53 (2).png\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>There are two that create free standing macros and 5 that are attached macros<\/li>\n\n\n\n<li>An expression is a piece of code that executes and presents a result. &nbsp;<\/li>\n\n\n\n<li>Detailed examples for each of these types of macros are gone thru in the session<\/li>\n\n\n\n<li>If two different macros are attached to the same code it doesn\u2019t matter about ordering, since they all are based on the original code.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Macro Implementation<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Comes after an = sign, and it is always another macro<\/li>\n\n\n\n<li>Usually an external macro implemented by a compiler plugin<\/li>\n\n\n\n<li>#externalMacro specifies the plugin to call and use to expand the code.<\/li>\n\n\n\n<li>You need to import SwiftSyntax &#8211; in your macro code to understand the Swift code syntax.&nbsp; Check out the <strong>Write Swift Macros<\/strong> session or the SwiftSyntax developer documentation.&nbsp;<\/li>\n\n\n\n<li>Import SwiftSyntaxMacros &#8211; protocols and types<\/li>\n\n\n\n<li>Import SwiftSyntaxBuilder &#8211; provides convince tools<\/li>\n\n\n\n<li>You should include comprehensive error codes for when someone tries to use it incorrectly&nbsp;<\/li>\n\n\n\n<li>You can provide Fix buttons, highlights and other information in your error message when the macro is used incorrectly<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Writing correct macros<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Name collision &#8211; you can use the makeUniqueName() method on the macro expansion context.<\/li>\n\n\n\n<li>\u25cf Swift needs to use names outside of the macro, so you need to use makeUniqueName() to solve this.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"blob:https:\/\/michaelrowe01.com\/8b0dbab1-6cda-4113-a388-331f238ea965\" alt=\"Screenshot 2023-06-07 at 18.04.31 (2).png\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Macros can only use the information that the compiler provides to them, so you can\u2019t create a macro that inserts the current date and time (as an example)<\/li>\n\n\n\n<li>Testing &#8211; it\u2019s an ordinary swift module so you should write normal swift unit test for it<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Why Macros Design Philosophy Translation Model Macro Roles Macro Implementation Writing correct macros<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_wp_convertkit_post_meta":{"form":"-1","landing_page":"0","tag":"0","restrict_content":"0"},"hide_page_title":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[2,3],"tags":[711,379,680],"class_list":["post-3146","post","type-post","status-publish","format-standard","hentry","category-blog","category-personal-softwareandit","tag-macros","tag-swift","tag-wwdc23"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2aMa8-OK","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/comments?post=3146"}],"version-history":[{"count":1,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3146\/revisions"}],"predecessor-version":[{"id":3149,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3146\/revisions\/3149"}],"wp:attachment":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/media?parent=3146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/categories?post=3146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/tags?post=3146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}