Slide 1

Slide 1 text

try! Swift macros Recap of WWDC23 Mercari https://mercari.connpass.com/event/285179/

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

What made me interested in Swift macros?

Slide 4

Slide 4 text

Twitter 🐤 https://twitter.com/ailtonvivaz/status/1666922079294201857

Slide 5

Slide 5 text

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)

Slide 6

Slide 6 text

😳

Slide 7

Slide 7 text

Agenda ● What is Swift macros ● Freestanding macros and Attached macros ● How to implement macros

Slide 8

Slide 8 text

try! したけど難しかった Swift macros Recap of WWDC23 Mercari https://mercari.connpass.com/event/285179/

Slide 9

Slide 9 text

What is Swift macros

Slide 10

Slide 10 text

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/

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

How to give Swift macros a try ● File > New > Package…

Slide 13

Slide 13 text

Package structure Declaration of a macro Implementation of a macro Test of a macro

Slide 14

Slide 14 text

Freestanding macros and Attached macros

Slide 15

Slide 15 text

Role in the context of Swift macros Cited from: https://developer.apple.com/videos/play/wwdc2023/10167?time=383

Slide 16

Slide 16 text

Available macro roles Cited from: https://developer.apple.com/videos/play/wwdc2023/10167?time=403 ● Freestanding macros: Can be used independently ● Attached macros: Can be used with some declaration

Slide 17

Slide 17 text

Use case of @freestanding(expression) Cited from: https://developer.apple.com/videos/play/wwdc2023/10167/?time=483

Slide 18

Slide 18 text

● 2 dimensional array Use case of @freestanding(declaration) Cited from: https://developer.apple.com/videos/play/wwdc2023/10167/?time=593 ● 3 dimensional array

Slide 19

Slide 19 text

Use case of @freestanding(declaration) Cited from: https://developer.apple.com/videos/play/wwdc2023/10167/?time=593

Slide 20

Slide 20 text

Use case of @attached(peer) Cited from: https://developer.apple.com/videos/play/wwdc2023/10167/?time=658

Slide 21

Slide 21 text

Use case of @attached(accessor) Cited from: https://developer.apple.com/videos/play/wwdc2023/10167/?time=735

Slide 22

Slide 22 text

Use case of @attached(memberAttribute) Cited from: https://developer.apple.com/videos/play/wwdc2023/10167/?time=822 Role composition

Slide 23

Slide 23 text

Use case of @attached(memberAttribute) Cited from: https://developer.apple.com/videos/play/wwdc2023/10167/?time=822 Role composition

Slide 24

Slide 24 text

Use case of @attached(member) Cited from: https://developer.apple.com/videos/play/wwdc2023/10167/?time=930

Slide 25

Slide 25 text

Use case of @attached(conformance) Cited from: https://developer.apple.com/videos/play/wwdc2023/10167/?time=1011

Slide 26

Slide 26 text

In the end…

Slide 27

Slide 27 text

How to implement macros

Slide 28

Slide 28 text

Package structure (Repeated) Declaration of a macro Implementation of a macro Test of a macro

Slide 29

Slide 29 text

#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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Swift AST Explorer ● A tool for visualizing AST ○ https://swift-ast-explorer.com/

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

😇

Slide 35

Slide 35 text

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!

Slide 36

Slide 36 text

Thank you

Slide 37

Slide 37 text

Appendix ● GitHub Gist ○ https://gist.github.com/tkhs0604/d2b6b1a84eeb3bead6faa6a6a1e6af33