{"data":{"post":{"title":"Swift Macro: Revisited - The Strengths and Essence","subtitle":"","isPublished":true,"createdTime":"2023-08-08T00:00:00.000Z","lastModifiedTime":null,"license":null,"tags":["Swift","Macro"],"category":"Programming","file":{"childMdx":{"excerpt":"From the sessions at WWDC 2023, we learned that Swift Macro aims to: Eliminate boilerplates Make…","code":{"body":"function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\nconst layoutProps = {};\nreturn class MDXContent extends React.Component {\n  constructor(props) {\n    super(props);\n    this.layout = null;\n  }\n\n  render() {\n    const _this$props = this.props,\n          {\n      components\n    } = _this$props,\n          props = _objectWithoutProperties(_this$props, [\"components\"]);\n\n    return React.createElement(MDXTag, {\n      name: \"wrapper\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `From the sessions at WWDC 2023, we learned that Swift Macro aims to:`), React.createElement(MDXTag, {\n      name: \"ul\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `Eliminate boilerplates`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `Make tedious things easy`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `Share with other developers in packages`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `However, these goals aren't unique to Swift Macro. They are common\nobjectives for many code reuse methods in Swift, such as functions, types,\nand modules. One could argue that all high-level programming languages\naspire to these ends. There must be something else that Swift Macro excels\nat; otherwise, it would be redundant.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `So, what is it that Swift Macro does exceptionally well?`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `The answer to this question is crucial. The unique strengths of Swift\nMacro defined its essence. It can guide us in crafting Swift macros that\nare effective, inform us of the boundaries when creating them, and\nultimately lead us to produce well-designed Swift macros.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `To grasp this, we need to first comprehend the problems that existing code\nreuse methods in Swift have managed to solve and their limitations. The\nkey to understanding what sets Swift Macro apart lies in this exploration.`), React.createElement(MDXTag, {\n      name: \"h2\",\n      components: components\n    }, `What Existing Code Reuse Methods Managed To Solve?`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `In Swift, the methods for code reuse encapsulate the code at various\nlevels of granularity, from the finest functions to the coarsest modules.\nThis design offers a hierarchical toolset to help us eliminate\nboilerplates in our code at each level.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `However, a well-designed code reuse method not only means to eliminate\nunnecessary copy-and-pastes but also brings order to chaos. From the\nhigh-level programming language developer's perspective, the chaos usually\ncomes from unexpected lookup results for the names of variables, functions\nor types and unexpected execution order of the program.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `More than that, real-world code reuse stories often involve codes provided\nby various parties. This means for the compiled programming languages the\ncode to reuse might be in variant distribution forms: either the source\ncode or prebuilt libraries.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Let's explore how existing Swift code reuse methods handle all these well.`), React.createElement(MDXTag, {\n      name: \"h3\",\n      components: components\n    }, `Functions`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Swift functions are the finest code reuse method in Swift. Programmers\nencapsulate basic algorithms by organizing sequences of execution flows\nwith control structures such as `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `if ... else ...`), ` statement, `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `while`), ` loop,\n`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `for`), ` loop and `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `return`), ` statement. Variables declared within the block of\na control structure have higher lookup priority than those declared\noutside. There is a piece of code that illustrates an example of this.\nEach line represents a name lookup result.`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `// A global variable decalred at the top level of a source file\nvar bar: Int = 0\n//   ^\n//   |\n//   +---------------------------------------------------+\nfunc foo() {                                          // |\n  func getHexDescription(for index: Int) -> String {  // |\n    return \"0x\" + String(index, radix: 16)            // |\n  }                                                   // |\n  for i in 0..<100 {                                  // |\n    // A local variable declared in a for loop           |\n    let bar = getHexDescription(for: i)               // |\n//       ^                                               |\n//       |                                               |\n//       +--------- +                                    |\n//                  |                                    |\n    print(\"bar = \\\\(bar)\")                             // |\n  }                                                   // |\n// +-----------------------------------------------------+\n// |\n  bar += 1\n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `This scene has become familiar in our daily programming. However, this\ndidn't come easy. The idea behind this design is called structured\nprogramming, defined by several key principles:`), React.createElement(MDXTag, {\n      name: \"ul\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `Variables are only accessible within the block of a control structure.`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `Control structures could be more expressive like using `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `if ... else ...`), `\ninstead of a simple `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `if`), ` statement.`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `The goto statement could be eliminated by more advanced control\nstructures.`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `The function could be defined within other functions.`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `The very first high-level programming language FORTRAN did not incorporate\nthese principles as we understand them today. It took the industry several\nyears to develop and integrate these concepts fully. The idea then\nsignificantly influenced the design and evolution of high-level\nprogramming languages after its introduction. Swift also embraced this\nidea to enable programmers to build robust functions.`), React.createElement(MDXTag, {\n      name: \"h3\",\n      components: components\n    }, `Types`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Swift types allow programmers to aggregate data and functions that share\ninternal relationships. This enables code reuse in a greater level of\ngranularity which is compared to standalone functions.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `However, simply packing a bunch of things into one does not eliminate the\ncomplexity. When accessing members declared in a type, there should be\nprinciples similar to the access rules for variables in structured\nprogramming. Such that we can ensure expected name lookup results. Or\nthere could be endless name collisions across types.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Yet, the internal data and the implementations should not be accessible\noutside a particular scope, such as the type or the source file, etc. This\ncould decrease the mind burden for the programmers that use the type by\nexposing only necessary information to the programmers, preventing\nundefined behaviors caused by unexpected access to the internal\nimplementations and defining a limited scope that the type can\ncollaborate.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Swift types overcome the aforementioned challenges by:`), React.createElement(MDXTag, {\n      name: \"ul\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `Type members such as variables and functions declared can be accessed\nwithin the block of the type definition.`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `Introducing the dot-notation syntax to access members outside the type\nblock.`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `Introducing access control to protect members in a type within a\nparticular scope.`)), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `struct Foo {\n\n  // \\`bar\\` is private\n  private var bar: Int\n\n  func print() {\n    // \\`bar\\` is acceessible within the block of type \\`Foo\\`.\n    print(bar)\n  }\n\n}\n\nlet foo = Foo()\n// \\`print\\` is acceessible via dot-notation syntax.\nfoo.print()\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `With all these points, programmers can intuitively expect the name lookup\nresults when working with types by generalizing what they learned from\nSwift functions that adopted principles of structured programming and\nbringing the experience got in other languages that have dot-notation\nsyntax and access control.`), React.createElement(MDXTag, {\n      name: \"h3\",\n      components: components\n    }, `Modules`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `From the previous section, we saw how Swift enhances code reusability with\ntypes when data and functions share internal relationships. How can we\nfurther improve reusability when types also share these relationships?`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `An intuitive answer is to further encapsulate types, data and functions\ninto an abstract at a greater granular level. In Swift, it is what the\nmodule does.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `However, in Swift, the module is not simply yet another aggregation of\ncode. Just like the type system, a naïvely designed module system may also\nintroduce name collisions across modules and expose unnecessary\nimplementation details. Swift avoids this by continually generalizing what\nwe learned from how Swift avoids similar problems in types:`), React.createElement(MDXTag, {\n      name: \"ul\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `Module members such as variables, functions and types can be accessed\nwithin the module.`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `Introducing the dot-notation syntax to access members outside the\nmodule when there are names in different modules collide.`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `Introducing access control to protect members in a module within a\nparticular scope.`)), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `// In the Foo module\n\nprivate var name = \"foo\"\n\npublic var version: String {\n  return \"1.0\"\n}\n\npublic func foo() {\n  print(name)\n}\n\n// In the Bar module\n\nprivate var name = \"bar\"\n\npublic var version: String {\n  return \"2.0\"\n}\n\npublic func bar() {\n  print(name)\n}\n\n// In the app\n\nimport Foo\nimport Bar\n\nprint(Foo.version)\nfoo()\nbar()\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `On top of that, the module is also a way to distribute your code. Since\npeople may distribute their code either in source code or prebuilt binary,\nthe Swift compiler may consider this while implementing the module system.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"figure\",\n      components: components,\n      parentName: \"p\"\n    }, `\n    `, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"figure\",\n      props: {\n        \"href\": \"/static/416ff74a809bcc6145b45758e39ca968/17031/potential-different-forms-of-distribution-for-modules.png\"\n      }\n    }, React.createElement(MDXTag, {\n      name: \"img\",\n      components: components,\n      parentName: \"a\",\n      props: {\n        \"title\": \"Potential different forms of distribution for modules.\",\n        \"alt\": \"Potential Different Forms of Distribution for Modules\",\n        \"src\": \"/static/416ff74a809bcc6145b45758e39ca968/17031/potential-different-forms-of-distribution-for-modules.png\",\n        \"srcSet\": [\"/static/416ff74a809bcc6145b45758e39ca968/17031/potential-different-forms-of-distribution-for-modules.png 1x\", \"/static/7d40b5fa4a7a7fe5197fa8858a5663be/6115b/potential-different-forms-of-distribution-for-modules%402x.png 2x\", \"/static/2d9e9773c1f4a8b1c661d1fbbafd9a16/907ec/potential-different-forms-of-distribution-for-modules%403x.png 3x\"],\n        \"loading\": \"lazy\"\n      }\n    })), `\n    `, React.createElement(MDXTag, {\n      name: \"figcaption\",\n      components: components,\n      parentName: \"figure\"\n    }, `\n        `, React.createElement(MDXTag, {\n      name: \"span\",\n      components: components,\n      parentName: \"figcaption\"\n    }, `\n            Potential different forms of distribution for modules.\n        `), `\n    `))), React.createElement(MDXTag, {\n      name: \"h3\",\n      components: components\n    }, `Recap`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `By examining the existing code reuse methods in Swift, we can find they\ntend to aggregate smaller abstractions into greater ones with a particular\nprotective mechanism to ensure reasonable name lookup results and expected\nprogram execution flow.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"figure\",\n      components: components,\n      parentName: \"p\"\n    }, `\n    `, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"figure\",\n      props: {\n        \"href\": \"/static/d31662b2d7d62db68d3e1872c9af98e3/61cee/established-trends-of-modules-types-and-functions.png\"\n      }\n    }, React.createElement(MDXTag, {\n      name: \"img\",\n      components: components,\n      parentName: \"a\",\n      props: {\n        \"title\": \"Established trends of modules/types and functions.\",\n        \"alt\": \"Established Trends of Modules/Types and Functions\",\n        \"src\": \"/static/d31662b2d7d62db68d3e1872c9af98e3/61cee/established-trends-of-modules-types-and-functions.png\",\n        \"srcSet\": [\"/static/d31662b2d7d62db68d3e1872c9af98e3/61cee/established-trends-of-modules-types-and-functions.png 1x\", \"/static/18f07f018ddb8ba0a3a23bdebeae0e24/ab540/established-trends-of-modules-types-and-functions%402x.png 2x\", \"/static/4da6c4d4c999ca77da47da20856c1574/0095f/established-trends-of-modules-types-and-functions%403x.png 3x\"],\n        \"loading\": \"lazy\"\n      }\n    })), `\n    `, React.createElement(MDXTag, {\n      name: \"figcaption\",\n      components: components,\n      parentName: \"figure\"\n    }, `\n        `, React.createElement(MDXTag, {\n      name: \"span\",\n      components: components,\n      parentName: \"figcaption\"\n    }, `\n            Established trends of modules/types and functions.\n        `), `\n    `))), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `However, what gives them advantages also caps their capabilities. Due to\nthose protective mechanisms, there are still programming concepts that\ncannot be encapsulated -- because some concepts require us to drop this\nprotection.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `To further improve the code reusability of the language, we no longer can\nfollow the established trends of these existing code reuse methods.`), React.createElement(MDXTag, {\n      name: \"h2\",\n      components: components\n    }, `What Swift Macros Do Exceptionally Well?`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Conversely, Swift Macro improved the code reusability of the language by\nembracing different design philosophies. To help you build a comprehensive\nunderstanding of these design philosophies, I would like to show you the\nnature of Swift Macro with some typical examples.`), React.createElement(MDXTag, {\n      name: \"h3\",\n      components: components\n    }, `Compile-Time Translations and Verifications`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Design software like Figma and Sketch represent the RGB color with 6\nhexadecimal digits. Developers often extend the type of color to allow\ndirect copying and pasting of RGB values from design software for use in\nSwift:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `import SwiftUI\n\n// Color extension\nextension Color {\n\n  public init(_ hex: UInt)\n\n}\n\n// Use example\nColor(0xFFEEAA)\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `But how do we verify that the pasted value is a valid RGB color? The\naction of copy-and-paste does not ensure the correctness of the result.\nThe following code snippet shows a potential mistake that results in a\ncopy-and-paste.`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `// Invalid color. 5 hexadecimal digits only\nColor(0xFFEEA)\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `However, because Swift macros syntactically transform their arguments to\ngenerate new code, or say \"to expand the macro\", we can integrate syntax\nchecking during this transformation. This enables compile-time\nverification for 6-digit hexadecimal RGB color \"literals\".`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `#Color(0xFFEEAA) // Compiled\n// #Color expansion began\nSwiftUI.Color(red: 1, green: 0.93, blue: 0.67)\n// #Color expansion ended\n\n#Color(0xFFEEA) // Invalid RGB, not compiled\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"figure\",\n      components: components,\n      parentName: \"p\"\n    }, `\n    `, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"figure\",\n      props: {\n        \"href\": \"/static/c138354b0751fd91824926ea476ffb16/08d94/invalid-rgb-literal.webp\"\n      }\n    }, React.createElement(MDXTag, {\n      name: \"picture\",\n      components: components,\n      parentName: \"a\"\n    }, `\n  `, React.createElement(MDXTag, {\n      name: \"source\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/c138354b0751fd91824926ea476ffb16/0cc25/invalid-rgb-literal.png\",\n        \"srcSet\": [\"/static/c138354b0751fd91824926ea476ffb16/5116e/invalid-rgb-literal.png 178w\", \"/static/c138354b0751fd91824926ea476ffb16/92f55/invalid-rgb-literal.png 356w\", \"/static/c138354b0751fd91824926ea476ffb16/0cc25/invalid-rgb-literal.png 712w\", \"/static/c138354b0751fd91824926ea476ffb16/7ae06/invalid-rgb-literal.png 1068w\", \"/static/c138354b0751fd91824926ea476ffb16/eee47/invalid-rgb-literal.png 1424w\", \"/static/c138354b0751fd91824926ea476ffb16/38407/invalid-rgb-literal.png 2136w\", \"/static/c138354b0751fd91824926ea476ffb16/240a0/invalid-rgb-literal.png 2144w\"],\n        \"sizes\": \"(max-width: 712px) 100vw, 712px\"\n      }\n    }), React.createElement(MDXTag, {\n      name: \"source\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/c138354b0751fd91824926ea476ffb16/690c8/invalid-rgb-literal.webp\",\n        \"srcSet\": [\"/static/c138354b0751fd91824926ea476ffb16/25c8a/invalid-rgb-literal.webp 178w\", \"/static/c138354b0751fd91824926ea476ffb16/60698/invalid-rgb-literal.webp 356w\", \"/static/c138354b0751fd91824926ea476ffb16/690c8/invalid-rgb-literal.webp 712w\", \"/static/c138354b0751fd91824926ea476ffb16/d7e52/invalid-rgb-literal.webp 1068w\", \"/static/c138354b0751fd91824926ea476ffb16/456ef/invalid-rgb-literal.webp 1424w\", \"/static/c138354b0751fd91824926ea476ffb16/2a654/invalid-rgb-literal.webp 2136w\", \"/static/c138354b0751fd91824926ea476ffb16/08d94/invalid-rgb-literal.webp 2144w\"],\n        \"sizes\": \"(max-width: 712px) 100vw, 712px\"\n      }\n    }), `\n  `, React.createElement(MDXTag, {\n      name: \"img\",\n      components: components,\n      parentName: \"picture\",\n      props: {\n        \"src\": \"/static/c138354b0751fd91824926ea476ffb16/08d94/invalid-rgb-literal.webp\",\n        \"alt\": \"Invalid RGB Literal That Does Not Compiled\",\n        \"title\": \"Invalid RGB Literal That Does Not Compiled\",\n        \"width\": 712,\n        \"height\": 313,\n        \"loading\": \"lazy\"\n      }\n    }))), `\n    `, React.createElement(MDXTag, {\n      name: \"figcaption\",\n      components: components,\n      parentName: \"figure\"\n    }, `\n        `, React.createElement(MDXTag, {\n      name: \"span\",\n      components: components,\n      parentName: \"figcaption\"\n    }, `\n            Invalid RGB Literal That Does Not Compiled\n        `), `\n    `))), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `From this example, you may be inspired to see how this compile-time\ncomputability can be applied to other types of \"literals\".`), React.createElement(MDXTag, {\n      name: \"h3\",\n      components: components\n    }, `Offering Behaviors Beyond The Functions`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `In practice, we typically aim to avoid using an exclamation mark to\nforcefully unwrap optional values:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `func foo(_ bar: Int?) {\n  print(bar!)\n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Instead, we prefer a safer approach -- use `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `guard let ... else`), `:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `func foo(_ bar: Int?) {\n  guard let bar else {\n    return\n  }\n  print(bar)\n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `However, this can be cumbersome, especially with multiple optional\nparameters.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Given a programmer's nature, there must be a desire to encapsulate this\nunwrapping process for reuse. Unfortunately, since a function protects its\ninternal execution flow from inner functions' return\n`, React.createElement(MDXTag, {\n      name: \"strong\",\n      components: components,\n      parentName: \"p\"\n    }, `for the sake of structured programming`), `, we cannot encapsulate this\n`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `guard let ... else`), ` in a function -- because the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `return`), ` statement in a\nfunction cannot make the caller site function exit.`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `func guard<T>(_ value: T?) -> T {\n  guard let value else {\n    // This return cannot help \\`foo\\` to exit.\n    // More than that, this function does not compile.\n    return\n  }\n  return value\n}\n\nfunc foo(_ bar: Int?) {\n  let bar = guard(bar)\n  print(bar)\n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `However, Swift Macro provides a feasible method for this type of\nencapsulation. We can have a macro called `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `#unwrap`), ` which has the\nfollowing use example:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `func foo(_ bar: Int?) {\n  #unwrap(bar) {\n    print(bar)\n  }\n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `This could be expanded as:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `func foo(_ bar: Int?) {\n  // #unwrap expansion began\n  guard let bar = bar else {\n    return\n  }\n  print(bar)\n  // #unwrap expansion ended\n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `In the example above, the arguments of the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `#unwrap`), ` macro -- `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `bar`), ` and\nthe trailing closure, are type-checked before the compiler initiates the\nmacro expansion process. This means the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `bar`), ` received by `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `print`), ` in the\ntrailing closure would be bound to the parameter `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `_ bar: Int?`), ` of the\n`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `foo`), ` function after the type-check.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `However, once the macro expanded, since the expansion process itself could\nbe seen as a syntax replacement much like the copy-and-paste, the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `bar`), `\nused by `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `print`), ` now is bound to the one declared by the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `guard let bar`), `\nstatement, being unrelated to the type-check result before the macro\nexpansion. More than that, the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `return`), ` statement brought by this macro\nexpansion can also affect the control flow of the applied site now.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `This example shows the evidence that the expansion of a freestanding Swift\nmacro could involve `, React.createElement(MDXTag, {\n      name: \"strong\",\n      components: components,\n      parentName: \"p\"\n    }, `control flow manipulation`), ` and\n`, React.createElement(MDXTag, {\n      name: \"strong\",\n      components: components,\n      parentName: \"p\"\n    }, `lexical scope sharing`), `.`), React.createElement(MDXTag, {\n      name: \"h3\",\n      components: components\n    }, `Offering Behaviors Beyond Types`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `The capabilities of Swift Macro are not bound to these boundaries. Let's\nconsider another kind of Swift Macro: attached macros and showcase its\npotential with a real-life example.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `In real-world programming, we often start with types with simple\ndefinitions. Here is an example:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `struct User {\n\n  var name: String\n\n  var avatar: URL\n\n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `However, as the repository grows, the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `struct`), `s might expand\nproportionally:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `struct User {\n\n  var name: String\n\n  var avatar: URL\n\n  var avatarsInDifferentScales: [Int : URL]\n\n  var userID: String\n\n  var socialMedias: [String]\n\n  var brief: String\n\n  // ...\n\n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Since each property in the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `struct`), ` of this example requires heap\nallocation for data storage, the cost of copying this struct also rises.\nThe number of heap allocations corresponds to the number of retaining\noperations during the copy. Since retaining is atomic, this could\npotentially cause lagging in user interactions and waste in memory space.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `To minimize the struct's copying cost, we can adopt copy-on-write behavior\nto the original struct by aggregating the properties into a class instance\nthat acts as the storage and copying the storage when altering the\nproperties:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `struct User {\n\n  // The heap storage type that aggregates the original stored properties\n  // in the struct.\n  private class Storage {\n\n    var name: String\n\n    // other properties ...\n\n  }\n\n  // The instance of the heap storage.\n  private var _storage: Storage\n\n  init(name: String, ...) {\n    self._storage = Storage(name: name, ...)\n  }\n\n  // Offers copy-on-write behavior.\n  private func makeStorageUniqueIfNeeded() {\n    if !isKnownUniquelyReferenced(&_storage) {\n      _storage = Storage(name: name, ...)\n    }\n  }\n\n  // A rewritten property in the struct.\n  var name: String {\n    get { return _storage.name }\n    set {\n      makeStorageUniqueIfNeeded()\n      _storage.name = newValue\n    }\n  }\n\n  // Other rewritten properties ...\n\n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `To understand this technique further, there is an illustration shows the\ndifference between the working details before and after adopting the\ncopy-on-write behavior:`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"figure\",\n      components: components,\n      parentName: \"p\"\n    }, `\n    `, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"figure\",\n      props: {\n        \"href\": \"/static/355742c47821c848e024407c6b9fb31f/1c292/working-details-before-and-after-adopting-the-copy-on-write-behavior.png\"\n      }\n    }, React.createElement(MDXTag, {\n      name: \"img\",\n      components: components,\n      parentName: \"a\",\n      props: {\n        \"title\": \"Working details before and after adopting the copy-on-write behavior.\",\n        \"alt\": \"Working Details Before and After Adopting The Copy-on-Write Behavior\",\n        \"src\": \"/static/355742c47821c848e024407c6b9fb31f/1c292/working-details-before-and-after-adopting-the-copy-on-write-behavior.png\",\n        \"srcSet\": [\"/static/355742c47821c848e024407c6b9fb31f/1c292/working-details-before-and-after-adopting-the-copy-on-write-behavior.png 1x\", \"/static/5bb29f579ffc5e90b1ace40439de1058/5db75/working-details-before-and-after-adopting-the-copy-on-write-behavior%402x.png 2x\", \"/static/dcb79b50d3c0bda474ea46f5bca29ae3/16891/working-details-before-and-after-adopting-the-copy-on-write-behavior%403x.png 3x\"],\n        \"loading\": \"lazy\"\n      }\n    })), `\n    `, React.createElement(MDXTag, {\n      name: \"figcaption\",\n      components: components,\n      parentName: \"figure\"\n    }, `\n        `, React.createElement(MDXTag, {\n      name: \"span\",\n      components: components,\n      parentName: \"figcaption\"\n    }, `\n            Working details before and after adopting the copy-on-write behavior.\n        `), `\n    `))), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `This is a useful technique to improve the performance when copying big\n`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `struct`), `s comes to be the bottleneck. In real-world testing, I improved\nthe performance of an app produced by ByteDance by adopting this\ntechnique, increasing the FPS of a particular scene from 48 to 56 and\nreducing the debug-time overall memory usage by 600MB.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `However, the approach I showed above can be cumbersome. It not only\ninvolves a lot of hand-roll code but, worse still, it increases the cost\nof maintaining the program.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `More than that, this technique cannot be encapsulated with any existing\nkind of types in Swift: no matter `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `class`), `, `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `struct`), `, `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `enum`), ` or `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `protocol`), `.\nThis is because what I've done to the struct is to extend the type's\nbehaviors by transforming the existing members into another form. Since we\ncan only reuse a type with aggregation, inheritance and conformance, none\nof this could help us encapsulate this piece of logic.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `But Swift Macro offers what we want. We can encapsulate this code\ntransformation process with Swift Macro and approach what I've done in the\nprevious example more elegantly.`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `@COW\nstruct User {\n\n  var name: String\n\n  var avatar: URL\n\n  var avatarsInDifferentScales: [Int : URL]\n\n  var userID: String\n\n  var socialMedias: [String]\n\n  var brief: String\n\n  // ...\n\n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `Yes. Just as simple as I've shown. By adding an attached macro called\n`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `@COW`), ` to the struct, we have introduced the copy-on-write behavior to\nthis struct.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `What this macro did is nothing more than what we have hand-rolled in the\nprevious example -- adding heap storage to the struct and transforming the\nstored properties into computed properties that forward access to the heap\nstorage:`), React.createElement(MDXTag, {\n      name: \"pre\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"code\",\n      components: components,\n      parentName: \"pre\",\n      props: {\n        \"className\": \"language-swift\"\n      }\n    }, `@COW\nstruct User {\n\n  // @COW expansion began\n  private class Storage {\n\n    var name: String\n\n    // other properties ...\n\n  }\n\n  private var _$storage: Storage\n\n  private func makeStorageUniqueIfNeeded() {\n    if !isKnownUniquelyReferenced(&_$storage) {\n      _$storage = Storage(name: name, ...)\n    }\n  }\n\n  init(name: String, ...) {\n    self._$storage = Storage(name: name, ...)\n  }\n  // @COW expansion ended\n\n  // @COW expansion began\n  @COWIncluded(storage: _$storage)\n  // @COW expansion ended\n  var name: String {\n    // @COWIncluded expansion began\n    get { return _$storage.name }\n    set {\n      makeStorageUniqueIfNeeded()\n      _$storage.name = newValue\n    }\n    // @COWIncluded expansion ended\n  }\n\n}\n`)), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `However, all these above happen in an automatic process that the Swift\ncompiler type-checks the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `User`), ` struct and then invokes the macro\nexpansion by using the type-checked `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `User`), ` struct as an argument. Finally,\nthe `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `@COW`), ` macro generates code by understanding the contents in the\n`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `User`), ` struct. With this automatic mechanism, the cost of maintenance has\nbeen eliminated.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `From the macro expansion shown above, it can be observed that attached\nSwift macros can extend types with members and rewrite properties by\nadding accessors. The extended contents not only bring new behaviors to\nthe type but also share the \"namespace\" of the extended point. It is also\nworth noting that adding accessors to a stored property also changed its\nsemantics from a stored property into a computed property.`), React.createElement(MDXTag, {\n      name: \"h2\",\n      components: components\n    }, `Conclusion`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `By studying the existing code reuse methods in Swift and understanding the\ncharacteristics of the newer Swift macros, we can draw the following\nconclusions:`), React.createElement(MDXTag, {\n      name: \"ul\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `Swift Macro is yet another form of encapsulation. It does not bring any\nnew runtime capabilities.`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `Swift Macro generates codes by transforming the programmer's code at the\ncompile time. This means that we can also integrate compile-time\nverification into it.`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `Unlike existing code reuse features in Swift, Swift Macro does not\nprotect its expansion from the existing contents of the applied site by\ndefault. Yet, it also can change the semantics of applied site. Macro\nauthors shall watch out for potential traps and pitfalls while\nimplementing Swift macros. To be specific:`, React.createElement(MDXTag, {\n      name: \"ul\",\n      components: components,\n      parentName: \"li\"\n    }, React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `For freestanding Swift macros, they can affect the control flow of the\napplied site as well as share the lexical scope.`), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, `For attached Swift macros, they can extend members to types as well as\naccessors to properties. The extended contents also share the same\n\"namespace\" of the extended point. More than that, accessor macros\ncould turn a stored property into a computed property by adding either\nthe `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `get`), `, `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `set`), ` or other undocumented accessors like `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `_read`), `,\n`, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `_modify`), `, `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `unsafeAddress`), ` and `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"li\"\n    }, `unsafeMutableAddress`), `.`)))), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"figure\",\n      components: components,\n      parentName: \"p\"\n    }, `\n    `, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"figure\",\n      props: {\n        \"href\": \"/static/59c4f742d8386703938c36fb1093fe1e/61cee/how-swift-macro-works.png\"\n      }\n    }, React.createElement(MDXTag, {\n      name: \"img\",\n      components: components,\n      parentName: \"a\",\n      props: {\n        \"title\": \"How Swift Macro works.\",\n        \"alt\": \"How Swift Macro Works\",\n        \"src\": \"/static/59c4f742d8386703938c36fb1093fe1e/61cee/how-swift-macro-works.png\",\n        \"srcSet\": [\"/static/59c4f742d8386703938c36fb1093fe1e/61cee/how-swift-macro-works.png 1x\", \"/static/f49705b982817146d84bfaf4c8518d00/ab540/how-swift-macro-works%402x.png 2x\", \"/static/b81b6bc64b6f6f46bc76930f4d50284f/0095f/how-swift-macro-works%403x.png 3x\"],\n        \"loading\": \"lazy\"\n      }\n    })), `\n    `, React.createElement(MDXTag, {\n      name: \"figcaption\",\n      components: components,\n      parentName: \"figure\"\n    }, `\n        `, React.createElement(MDXTag, {\n      name: \"span\",\n      components: components,\n      parentName: \"figcaption\"\n    }, `\n            How Swift Macro works.\n        `), `\n    `))), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `These properties offer programmers not only another option for code reuse\nbut also the ability to encapsulate programming concepts that involve\ncompile-time checking, control flow manipulations, and adding behaviors to\ntypes without relying on inheritance or other runtime techniques. These\nproperties have never been implemented in Swift before. Without a doubt,\nthey are the unique strengths that define Swift Macro.`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components\n    }, `However, the same features that give Swift Macro its advantages also\nintroduce potential traps and pitfalls. We will delve into this topic in\nthe following post.`), React.createElement(MDXTag, {\n      name: \"h2\",\n      components: components\n    }, `Resources`), React.createElement(MDXTag, {\n      name: \"ul\",\n      components: components\n    }, React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, React.createElement(MDXTag, {\n      name: \"p\",\n      components: components,\n      parentName: \"li\"\n    }, `A playground project that implements the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `#Color`), ` and `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `#unwrap`), `\nmacro (needs `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `git checkout strengths-and-essence`), `)`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components,\n      parentName: \"li\"\n    }, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"p\",\n      props: {\n        \"href\": \"https://github.com/WeZZard/SwiftMacroRevisited\"\n      }\n    }, `WeZZard/SwiftMacroRevisited`))), React.createElement(MDXTag, {\n      name: \"li\",\n      components: components,\n      parentName: \"ul\"\n    }, React.createElement(MDXTag, {\n      name: \"p\",\n      components: components,\n      parentName: \"li\"\n    }, `The production level implementation of the `, React.createElement(MDXTag, {\n      name: \"inlineCode\",\n      components: components,\n      parentName: \"p\"\n    }, `@COW`), ` macro:`), React.createElement(MDXTag, {\n      name: \"p\",\n      components: components,\n      parentName: \"li\"\n    }, React.createElement(MDXTag, {\n      name: \"a\",\n      components: components,\n      parentName: \"p\",\n      props: {\n        \"href\": \"https://github.com/wezzard/cowmacro\"\n      }\n    }, `WeZZard/COWMacro`)))));\n  }\n\n}\nMDXContent.isMDXComponent = true;","scope":""},"headings":[{"value":"What Existing Code Reuse Methods Managed To Solve?","depth":2},{"value":"Functions","depth":3},{"value":"Types","depth":3},{"value":"Modules","depth":3},{"value":"Recap","depth":3},{"value":"What Swift Macros Do Exceptionally Well?","depth":2},{"value":"Compile-Time Translations and Verifications","depth":3},{"value":"Offering Behaviors Beyond The Functions","depth":3},{"value":"Offering Behaviors Beyond Types","depth":3},{"value":"Conclusion","depth":2},{"value":"Resources","depth":2}]}}},"earlierPostExcerpt":{"slug":"/post/2023/03/adapting-reference-semantics-model-in-swiftui-the-basics-f521","title":"Adapting Reference Semantics Model in SwiftUI - The Basics","subtitle":"","createdTime":"2023-03-02T00:00:00.000Z","tags":["SwiftUI","Swift","Adaptor","Reference Semantics","Binding"],"category":"Programming","file":{"childMdx":{"excerpt":"Introduction Recently, one of my colleagues had been struggling with porting reference semantics model to SwiftUI with  ObservableObject  and  @StateObject . In this post, since there are many examples talked about porting reference semantics models to SwiftUI this way on the Internet, I'm not going…"}}},"laterPostExcerpt":{"slug":"/post/2023/08/swift-macro-revisited-traps-and-pitfalls-1034","title":"Swift Macro: Revisited - Traps and Pitfalls","subtitle":"","createdTime":"2023-08-10T00:00:00.000Z","tags":["Swift","Macro"],"category":"Programming","file":{"childMdx":{"excerpt":"In the previous post, we learned the strengths and the essence that\nuniquely define the Swift Macro. The examples in that post work so far so\ngood. However, can we be confident and bold, implementing any Swift macros\nwe want now? No. The features that bring Swift Macro advantages also introduce…"}}}},"pageContext":{"postId":"f2b372f7-8c6e-5cc2-9792-84dc1fac5aa9","earlierPostId":"e508707d-f31e-5d24-b1ac-2b6a1b6155fc","laterPostId":"5dad251c-7c9d-519a-89a1-db4f32ea4fcb"}}