Upgrade to Pro — share decks privately, control downloads, hide ads and more …

try! Swift macros

June 30, 2023

try! Swift macros


June 30, 2023

More Decks by kenken

Other Decks in Technology


  1. About me • Nickname ◦ kenken • Company ◦ Merpay,

    Inc. (since May, 2021) • Occupation ◦ Software Engineer, Android (and iOS at some of my previous companies) • Accounts ◦ Twitter: @tkhs0604 ◦ GitHub: @tkhs0604 ◦ Bluesky: @kenken.bsky.social • Other ◦ DroidKaigi community staff
  2. Comparison between Swift and Kotlin codes • Swift code with

    SwiftRequest (https://github.com/ailtonvivaz/swift-request) • Kotlin code with Retrofit (https://github.com/square/retrofit)
  3. Agenda • What is Swift macros • Freestanding macros and

    Attached macros • How to implement macros
  4. Swift macros • A feature added from Swift 5.9 ◦

    You can use it after Xcode 15.0 beta • A tool for generating boilerplate codes repetitively written by hand • #Preview, @Observable etc. are created with Swift macros Cited from: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/macros/
  5. Swift macros • Transforms the syntax tree of a source

    code at compile time ◦ Input code to a macro and output code of macro expansion are validated syntactically ◦ Values passed to a macro and in the code generated by a macro are type-checked ◦ If the implementation of a macro has any error, Swift compiler treats it as a compilation error Cited from: https://developer.apple.com/videos/play/wwdc2023/10167/?time=291
  6. • 2 dimensional array Use case of @freestanding(declaration) Cited from:

    https://developer.apple.com/videos/play/wwdc2023/10167/?time=593 • 3 dimensional array
  7. #externalMacro • Specifies the plug-in (module) which the compiler should

    launch and the name of a type inside the plug-in • When Swift expands the following macro, #stringify(_:) will launch a plug-in called "MyLibMacros" and ask a type called "StringifyMacro" to expand it Cited from: https://developer.apple.com/videos/play/wwdc2023/10167/?time=1102
  8. Macro role protocols • Used to create a macro implementation

    ◦ https://github.com/apple/swift-syntax/tree/main/Sources/SwiftSyntaxMacros/MacroProtocols Cited from: https://developer.apple.com/videos/play/wwdc2023/10167/?time=1356
  9. Abstract syntax tree (AST) • A tree representation of the

    abstract syntactic structure of a source code • SwiftSyntax is a library that helps us parse, inspect, manipulate, and generate Swift source code ◦ https://github.com/apple/swift-syntax Cited from: https://developer.apple.com/videos/play/wwdc2023/10167/?time=1288
  10. Sample of a macro implementation • Implemented as an extension

    in this example ◦ https://github.com/DougGregor/swift-macro-examples/blob/main/MacroExamplesPlugin/Dictio naryIndirectionMacro.swift#L63-L84
  11. Summary • Swift macros is a tool for generating boilerplate

    codes • Inputs and outputs are syntax-checked and type-checked at compile time • Some useful macros, like #preview and @Observable have already been introduced • SwiftSyntax is an essential tool for implementing macros ◦ Quite difficult to understand… 😇 • So… let’s try to create macros by yourself and share knowledges!