Make Swift Macros Write Code for You | Swift Macros Deep Dive (Part 2)
The second part of this Swift Macros Deep Dive covers the process of writing, testing, and debugging Swift macros in detail. In the video, I solve three different tasks in BirdChat (my demo project) with the help of three different flavors of macros: • a declaration (freestanding) macro allows me to radically reduce the amount of boilerplate when declaring ready-to-present enums—the declarations now take just one line; • an attached macro helps with conforming types to the Equatable protocol in a custom way—but without writing much code; • and an expression (freestanding) macro makes it much easier to keep track of what still needs to be done in the project—and implement the missing pieces when I’m ready. Besides, I demonstrate how you start from scratch and create a macros package, and how to debug and test the macros to make sure they work just as expected. Also watch the first part where I explain why macros were added to Swift, how they work, how their flavors differ from each other, and—most importantly—how you can use them to make your code shorter and safer: https://youtu.be/LrY9f5kOQ_4 Support the Swift Bird by… • …joining as a member: https://www.youtube.com/channel/UC3mDnPzYxvKHaMLANaiDDYQ/join • …becoming a patron: https://url.ykvm.com/patreon • …buying me a coffee: https://url.ykvm.com/bmac Links: • The BirdChat app for you to practice (includes branches with a starter template and complete project): https://github.com/TheSwiftBird/BirdChat • Macros documentation: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/macros/ • Swift Syntax: https://github.com/apple/swift-syntax • Attached macros’ attributes: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/attributes/#attached • Applying macros: https://developer.apple.com/documentation/Swift/applying-macros • Freestanding (expression) macros: https://github.com/apple/swift-evolution/blob/main/proposals/0382-expression-macros.md • Freestanding (declaration) macros: https://github.com/apple/swift-evolution/blob/main/proposals/0397-freestanding-declaration-macros.md • Attached macros: https://github.com/apple/swift-evolution/blob/main/proposals/0389-attached-macros.md • Macros examples: https://github.com/apple/swift-syntax/tree/main/Examples • Curated collection of Swift macros: https://github.com/krzysztofzablocki/Swift-Macros Chapters: • 0:00 Intro • 0:47 What’s the Deal with Writing Swift Macros? • 1:33 Project Overview • 2:39 Adding a Macros Target • 4:24 Freestanding Macro for Declaring Enums with One Line of Code • 12:42 Attached Macro for Inferring Equatable Conformance • 15:28 Expression Macro for Highlighting TODOs • 18:35 Debugging & Testing Macros • 23:29 One More Thing: Replacing a String Literal with a Syntax Expression • 24:20 My Impressions of Writing Swift Macros • 25:11 Recap • 27:14 Outro The Swift Bird, a Yakov Manshin production 🎞️ #Swift #macros #softwareengineering
Download
0 formatsNo download links available.