diff --git a/src/assets/json/en/ios/wwdc/ios-wwdc-listnota11y.json b/src/assets/json/en/ios/wwdc/ios-wwdc-listnota11y.json index 19f217d91..860f0e48a 100644 --- a/src/assets/json/en/ios/wwdc/ios-wwdc-listnota11y.json +++ b/src/assets/json/en/ios/wwdc/ios-wwdc-listnota11y.json @@ -10,6 +10,17 @@ "raccourcis": "", "profils": ["All","Accessibility","Dark Mode","Debugging","DocC","Formatting","Instruments","Keyboard","Keynote","Localization","Optimization","Platforms State...","Regex","Shortcuts","Swift","Swift Playgrounds","SwiftUI","What's new in...","Xcode"] }, + { + "themes": "WWDC 2023", + "title": "Expand on Swift macros", + "type": [], + "tests": [], + "verifier": [], + "resultat": [], + "exception": "", + "raccourcis": "2023/23SwiftMacros", + "profils": ["All","Swift"] + }, { "themes": "WWDC 2023", "title": "Generalize APIs with parameter packs", @@ -120,6 +131,17 @@ "raccourcis": "2023/2310058", "profils": ["All","What's new in..."] }, + { + "themes": "WWDC 2023", + "title": "Write Swift macros", + "type": [], + "tests": [], + "verifier": [], + "resultat": [], + "exception": "", + "raccourcis": "2023/23SwiftMacros", + "profils": ["All","Swift"] + }, { "themes": "WWDC 2022", "title": "Adopt Variable Color in SF Symbols", diff --git a/src/en/mobile/images/iOSdev/wwdc23-10168_5.png b/src/en/mobile/images/iOSdev/wwdc23-10168_5.png index bf1349758..6aa1a644e 100644 Binary files a/src/en/mobile/images/iOSdev/wwdc23-10168_5.png and b/src/en/mobile/images/iOSdev/wwdc23-10168_5.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_1.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_1.png new file mode 100644 index 000000000..c4896eef0 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_1.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_10.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_10.png new file mode 100644 index 000000000..bb835c0a3 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_10.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_11.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_11.png new file mode 100644 index 000000000..cf1229d16 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_11.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_12.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_12.png new file mode 100644 index 000000000..b4e407763 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_12.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_13.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_13.png new file mode 100644 index 000000000..7c98a3174 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_13.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_14.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_14.png new file mode 100644 index 000000000..f4f0508b4 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_14.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_15.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_15.png new file mode 100644 index 000000000..8f3004cf9 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_15.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_16.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_16.png new file mode 100644 index 000000000..92b50e3f5 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_16.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_17.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_17.png new file mode 100644 index 000000000..11ed2e2c3 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_17.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_18.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_18.png new file mode 100644 index 000000000..7dda2d78a Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_18.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_19.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_19.png new file mode 100644 index 000000000..f48a26d33 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_19.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_2.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_2.png new file mode 100644 index 000000000..378cab383 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_2.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_20.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_20.png new file mode 100644 index 000000000..743c35e94 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_20.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_21.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_21.png new file mode 100644 index 000000000..4abf38a7f Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_21.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_22.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_22.png new file mode 100644 index 000000000..5d5f4907d Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_22.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_23.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_23.png new file mode 100644 index 000000000..c0cf787a9 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_23.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_24.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_24.png new file mode 100644 index 000000000..0420e6f87 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_24.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_25.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_25.png new file mode 100644 index 000000000..2f92d1a86 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_25.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_26.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_26.png new file mode 100644 index 000000000..7b1d4aed2 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_26.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_27.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_27.png new file mode 100644 index 000000000..77dff8bb2 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_27.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_3.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_3.png new file mode 100644 index 000000000..25d75c05f Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_3.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_4.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_4.png new file mode 100644 index 000000000..d4d1e1206 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_4.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_5.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_5.png new file mode 100644 index 000000000..29bcb5604 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_5.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_6.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_6.png new file mode 100644 index 000000000..768e34b67 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_6.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_7.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_7.png new file mode 100644 index 000000000..e1439a228 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_7.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_8.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_8.png new file mode 100644 index 000000000..b7a3aaeb9 Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_8.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_9.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_9.png new file mode 100644 index 000000000..31316fedf Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_9.png differ diff --git a/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_Poster.png b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_Poster.png new file mode 100644 index 000000000..335fae93b Binary files /dev/null and b/src/en/mobile/images/iOSdev/wwdc23-SwiftMacros_Poster.png differ diff --git a/src/en/mobile/ios/wwdc/nota11y/2023/23SwiftMacros.md b/src/en/mobile/ios/wwdc/nota11y/2023/23SwiftMacros.md new file mode 100644 index 000000000..19c45884e --- /dev/null +++ b/src/en/mobile/ios/wwdc/nota11y/2023/23SwiftMacros.md @@ -0,0 +1,310 @@ +--- +title : "Swift macros" +abstract: "Discover how Swift macros can help you reduce boilerplate in your codebase and adopt complex features more easily." + +displayToc: true +--- + +# WWDC 2023: Swift macros +Find hereafter a detailed summary of two videos that belong to a [taxonomy of some WWDC footages](../../). + +The original videos is available on the **Apple official website** ([session 10166](https://developer.apple.com/videos/play/wwdc2023/10166/), [session 10167](https://developer.apple.com/videos/play/wwdc2023/10167/)). + +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_Poster.png) + +"Discover how **Swift macros** can help you **reduce boilerplate** in your codebase and **adopt complex features** more easily. Learn how macros can analyze code, emit rich compiler errors to guide developers towards correct usage, and generate new code that is automatically incorporated back into your project. We'll also take you through important concepts like **macro roles**, **compiler plugins**, and **syntax trees**." + +"Discover how you can use Swift macros to make your codebase **more expressive** and **easier to read**. Code along as we explore how macros can help you **avoid writing repetitive code** and find out how to use them in your app. We'll share the **building blocks** of a macro, show you how to **test** it, and take you through how you can **emit compilation errors** from macros." +
Hereafter, the underlined elements lead directly to the playback of the WWDC video at the appropriate moment. +
+ +## [The basics](https://developer.apple.com/videos/play/wwdc2023/10167/?time=46) +The [numerous reasons](https://developer.apple.com/videos/play/wwdc2023/10167/?time=120) for introducing the **macros with `Swift` `5.9`** are all different but head alltogether towards the **simplicity of code**. +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_1.png) + +The **design goals** of these macros are fourthfold. + +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_5.png) + + + +
+
+ +🎬 + +Two kinds of macros may be used: the **freestanding** macros... +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_2.png) +... and the **attached** macros. +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_3.png) + +
+ +
+ +🎬 + +Before using a macro, the **incoming parameters** are validated and the **error warnings** may be taken in to account. +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_4.png) +
+ +
+ +🎬 + +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_6.png) +
+ +
+ +🎬 + +The code behind a macro may be reviewed thanks to the **`Xcode` new features**. +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_7.png) +
+
+
+
+ +A dedicated [compiler plugin](https://developer.apple.com/videos/play/wwdc2023/10167/?time=291) is assigned to the **macro expansion** that's added as code to the program logic. + +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_8.png) + +And all that new functioning relies on a basic **[macro declaration](https://developer.apple.com/videos/play/wwdc2023/10167/?time=339)**. +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_9.png) +
+ +## [The roles](https://developer.apple.com/videos/play/wwdc2023/10167/?time=376) +The set of rules that handle responsabilities areas of a macro is called a **role**. +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_10.png) +The roles for creating **attached** and **freestanding** macros are listed below. + +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_11.png) + + +
+
+ +🎬 + +This role is made for creating a piece of code that returns a value. + +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_12.png) +
+ +
+ +🎬 + +This role is used to create one or more declarations. +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_13.png) +
+ +
+ +🎬 + +This role adds extra declarations to the existing ones. +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_14.png) +
+ +
+ +🎬 + +This role is meant for macros that handle variables and subscripts for creating accessors. +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_15.png) +
+ +
+ +🎬 + +The goal of this role is to add some attributes to an existing type. +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_17.png) + +The **role composition** highlighted in this example is explained below, after the macro roles introduction. +
+ +
+ +🎬 + +Through this role, new declarations may be added to the type the macro is applied to. +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_18.png) +
+ +
+ +🎬 + +This role aims at adding a conformance to a protocol. +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_19.png) +
+
+
+
+ +The **[role composition](https://developer.apple.com/videos/play/wwdc2023/10167/?time=835)** should be used for creating macros in order to enrich their range usage and to avoid boilerplate code. + +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_16.png) +
+ +## [The implementation](https://developer.apple.com/videos/play/wwdc2023/10167/?time=1067) +When a macro has to be used, the [compiler creates a process](https://developer.apple.com/videos/play/wwdc2023/10167/?time=1110) in a secure sandbox where a plugin is launched to get the original code through a potential *[external macro](https://developer.apple.com/videos/play/wwdc2023/10167/?time=1117)*. +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_20.png) + +The source code is then parsed into a **[SwiftSyntax tree](https://developer.apple.com/videos/play/wwdc2023/10166?time=227)**... +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_21.png) + +... that highlights its [own types](https://developer.apple.com/videos/play/wwdc2023/10167/?time=1192) including **nodes** that may be [tokens](https://developer.apple.com/videos/play/wwdc2023/10167/?time=1233) or just have child nodes as properties. + +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_23.png) +
+ +### Libraries +To fully support the `SwiftSyntax tree`, dedicated libraires should be imported: +- **[SwiftSyntax](https://developer.apple.com/videos/play/wwdc2023/10167/?time=1177)** representing the source code as a tree structure. +- **[SwiftSyntaxMacros](https://developer.apple.com/videos/play/wwdc2023/10167/?time=1324)** dealing with necessary types and protocols for macros. +- **[SwiftSyntaxBuilder](https://developer.apple.com/videos/play/wwdc2023/10167/?time=1331)** supporting the new generated code through the syntax trees. + +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_22.png) + +### [Correct writing](https://developer.apple.com/videos/play/wwdc2023/10167/?time=2018) +Unlike many languages that hold [hygienic macro systems](https://developer.apple.com/videos/play/wwdc2023/10167/?time=2086), `Swift` handles the **[name collisions](https://developer.apple.com/videos/play/wwdc2023/10167/?time=2021)** through a dedicated method to be used. +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_24.png) + +Moreover, the **names specifiers** that can be used as arguments in macros are [fivefold](https://developer.apple.com/videos/play/wwdc2023/10167/?time=2162). +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_25.png) + +Should the **macros be reliable**, it's important [some rules](https://developer.apple.com/videos/play/wwdc2023/10167/?time=2236) be followed so as not to encounter outlandish behaviors. +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_26.png) +
+ +### [Testing](https://developer.apple.com/videos/play/wwdc2023/10167/?time=2293) +Testing macros should be one of the **essential milestones** of the development that relies on a **simple code structure**. +![](../../../../../images/iOSdev/wwdc23-SwiftMacros_27.png) +
+ +## Detailed example +Before diving into the different steps for creating a macro, let's look into the `Xcode` environment to find out some new features regarding this context. + +- Create a **macro template** ⟹ 🎬 + +- Test a macro through **unit tests** ⟹ 🎬 + +
+ +The [following phases](https://developer.apple.com/videos/play/wwdc2023/10166/?time=708) are meant to be automatically generating an initializer with a macro: + +- **Declare** a macro ⟹ 🎬 + +- **Implement** a macro ⟹ 🎬 + +- Connect an `attached macro` with its corresponding **MemberMacro protocol** ⟹ 🎬 + +- Write the **test case first** ⟹ 🎬 + +- Inspect the **SwiftSyntax tree** of an enum through its syntactic structure ⟹ 🎬 + +- Use the `Xcode` **breakpoint** for macros ⟹ 🎬 + +- Add the **macro package** to the Xcode project ⟹ 🎬 + +- **Expand a macro** to find out what its content is ⟹ 🎬 + +- Emit an **error message** and use the **diagnostics** ⟹ 🎬 + +- Insert **macro errors** with the `Swift Error protocol` ⟹ 🎬 + +
diff --git a/src/en/mobile/ios/wwdc/nota11y/index.md b/src/en/mobile/ios/wwdc/nota11y/index.md index 142cadce6..cf05423f4 100644 --- a/src/en/mobile/ios/wwdc/nota11y/index.md +++ b/src/en/mobile/ios/wwdc/nota11y/index.md @@ -24,16 +24,18 @@ The videos that may be of interest to **designers** are highlighted with a **ded