Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Swift macrosの入門ハードルは意外と低いかも
Search
stotic-dev
July 23, 2025
Technology
95
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Swift macrosの入門ハードルは意外と低いかも
stotic-dev
July 23, 2025
More Decks by stotic-dev
See All by stotic-dev
低コストで始めるiOSアプリ開発のVRT
stoticdev
0
100
SwiftUIとMetalで簡単に作るレアカード風UI
stoticdev
1
200
Swift Testingのモチベを上げたい
stoticdev
2
440
PRレビューのお供にDanger
stoticdev
1
440
Other Decks in Technology
See All in Technology
事業成長とデータ活用を止めないためのマネジメントへ挑戦
hiracky16
0
410
CTO Night|NotionのAIエージェントとFDE
kzkhykw1991
0
260
コンテナ運用におけるネットワーク・コンテナコストの削減方法/How to Reduce Network and Container Costs in Container Operations
masakiokuda
2
320
NDIでApple Vision Proにリアルタイム映像送信
jyouryuusui
0
130
What Is JetBrains Air
x5gtrn
PRO
0
110
Claude の新機能を、 出た日に触って社内に届けるまで
rfdnxbro
1
190
PHPカンファレンス関西の招待状 / Invitation to PHP Conference Kansai
ykanoh
0
220
今さら理解するウェブフォントver.2026
takanorip
3
930
フロントエンドカンファレンス関西2026 - 速すぎるUIに、待ち時間を
teamlab
PRO
0
200
Metadata - Section 1: HTML Basics
mohab1
0
130
JAWSUG TOHOKU 青森支部 「LoGoチャット」のAWS移行で 乗り越えたこと
taketakekaho
0
350
トークン化預金DCJPYの取り組みと活用ユースケース
finengine
0
190
Featured
See All Featured
Designing Experiences People Love
moore
143
24k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
580
We Have a Design System, Now What?
morganepeng
55
8.4k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.3k
Exploring anti-patterns in Rails
aemeredith
4
540
The Cost Of JavaScript in 2023
addyosmani
55
10k
A Tale of Four Properties
chriscoyier
163
24k
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.2k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.4k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
570
Darren the Foodie - Storyboard
khoart
PRO
4
4k
Transcript
Swift macrosೖͷϋʔυϧҙ֎ͱ͍͔ 0
ࣗݾհ ࠤ౻ଡҰ/ͯ͌ʔ ͓ࣄɿiOSΤϯδχΞ 1
Swift macrosͱʁ ίϯύΠϧ࣌ʹίʔυΛࣗಈੜ͢Δػೳ ܁Γฦ͠ॻ͔ΕΔఆܕίʔυΛݮ SwiftSyntaxͷߏจղੳɾੜͷػೳΛͬͯಈ͍͍ͯΔ 2
Swift macrosͱʁ 2
Swift macrosͱʁ 2
Swift macrosͱʁ expansionϝιουͰɺSwiftSyntaxʹΑͬͯղੳ͞ΕͨߏจΛͬͯίʔυΛੜ ͠·͢ɻ 2
Swift macrosɺͦ͠͏ ೖ͢ΔͷʹSwiftSyntaxͷ͕ࣝඞཁͦ͏ 3
Swift macrosɺͦ͠͏ SwiftSyntax͕Θ͔Βͳ͍ɺɺ SwiftSyntaxා͍ɺɺ SwiftSyntaxͬͯͳʹɺɺ 3
Swift macrosɺͦ͠͏ ೖ͢ΔͷʹSwiftSyntaxͷࣝͦΜͳʹඞཁͳ ͔ͬͨʂ 3
ϚΫϩ࡞ͷجຊεςοϓ Swift macrosೖྗͱग़ྗ͕͋Δͱ͍͏ͰؔͱࣅͨΑ͏ͳͷͰ͢ɻ ͳͷͰɺ࣮ͷεςοϓؔͱಉ͡Α͏ʹߟ͑Δ͜ͱ͕Ͱ͖·͢ɻ 4
ϚΫϩ࡞ͷجຊεςοϓ ೖྗΛܾΊΔ @modelConvert(SampleModel.self) struct SampleData { let id: String let
text: String } 4
ϚΫϩ࡞ͷجຊεςοϓ ग़ྗΛܾΊΔ extension SampleData: ModelConvertible { typealias ModelType = SampleModel
init(_ model: SampleModel) throws { id = model.id text = model.text } func toModel() throws -> SampleModel { return .init(id: id, text: text) } } 4
ϚΫϩ࡞ͷجຊεςοϓ ೖྗΛSwiftSyntaxͰղੳ͢Δ public static func expansion( of node: AttributeSyntax, attachedTo
declaration: some DeclGroupSyntax, providingExtensionsOf type: some TypeSyntaxProtocol, conformingTo protocols: [TypeSyntax], in context: some MacroExpansionContext ) throws -> [ExtensionDeclSyntax] { ... } 4
ϚΫϩ࡞ͷجຊεςοϓ ग़ྗΛSwiftSyntaxͰੜ͢Δ ExtensionDeclSyntax(...) 4
"ߏจͷน"ΛΓӽ͑Δ SwiftSyntaxͰίʔυߏจͱͯ͠ද͞Ε͍ͯ·͢ɻ ߏจʹSwiftSyntax⁶Swiftίʔυͷ૬ޓมΛॿ͚ΔͨΊͷඇৗʹ༗༻ͳใ͕٧ ·͍ͬͯ·͢ɻ ߏจΛݟΔϙΠϯτΛ͑ΕɺϚΫϩ࣮ͷϋʔυϧ͕άοͱԼ͕Γ·͢ɻ 5
ߏจ͔ΒҙͷใΛऔಘ͢Δ ߏจͱSyntaxͷΦϒδΣΫτͷϓϩύςΟ͕ಉ͡Ͱ͋Δ͜ͱ͕Θ͔Εɺղੳ؆ ୯Ͱ͢ɻ AttributeSyntax ᵓᴷatSign: atSign ᵓᴷattributeName: IdentifierTypeSyntax │ ‹ᴷname:
identifier("ModelConvert") ᵓᴷleftParen: leftParen ᵓᴷarguments: LabeledExprListSyntax │ ‹ᴷ<0>: LabeledExprSyntax │ ‹ᴷexpression: MemberAccessExprSyntax │ ᵓᴷbase: DeclReferenceExprSyntax │ │ ‹ᴷbaseName: identifier("SampleModel") │ ᵓᴷperiod: period │ ‹ᴷdeclName: DeclReferenceExprSyntax │ ‹ᴷbaseName: keyword(SwiftSyntax.Keyword.self) ‹ᴷrightParen: rightParen @modelConvert(SampleModel.self) 6
"baseName"ͷऔಘྫ AttributeSyntax ᵓᴷ... ‹ᴷarguments: LabeledExprListSyntax ‹ᴷ<0>: LabeledExprSyntax ‹ᴷexpression: MemberAccessExprSyntax ᵓᴷbase:
DeclReferenceExprSyntax │ ‹ᴷbaseName: identifier("SampleModel") ᵓᴷperiod: period ‹ᴷdeclName: DeclReferenceExprSyntax… node.arguments!.cast(LabeledExprListSyntax.self).first! .expression.as(MemberAccessExprSyntax.self)? .base?.as(DeclReferenceExprSyntax.self)? .baseName 6
"baseName"ͷऔಘྫ AttributeSyntax ᵓᴷ... ‹ᴷarguments: LabeledExprListSyntax ‹ᴷ<0>: LabeledExprSyntax ‹ᴷexpression: MemberAccessExprSyntax ᵓᴷbase:
DeclReferenceExprSyntax │ ‹ᴷbaseName: identifier("SampleModel") ᵓᴷperiod: period ‹ᴷdeclName: DeclReferenceExprSyntax… node.arguments!.cast(LabeledExprListSyntax.self).first! .expression.as(MemberAccessExprSyntax.self)? .base?.as(DeclReferenceExprSyntax.self)? .baseName 6
"baseName"ͷऔಘྫ AttributeSyntax ᵓᴷ... ‹ᴷarguments: LabeledExprListSyntax ‹ᴷ<0>: LabeledExprSyntax ‹ᴷexpression: MemberAccessExprSyntax ᵓᴷbase:
DeclReferenceExprSyntax │ ‹ᴷbaseName: identifier("SampleModel") ᵓᴷperiod: period ‹ᴷdeclName: DeclReferenceExprSyntax… node.arguments!.cast(LabeledExprListSyntax.self).first! .expression.as(MemberAccessExprSyntax.self)? .base?.as(DeclReferenceExprSyntax.self)? .baseName 6
"baseName"ͷऔಘྫ AttributeSyntax ᵓᴷ... ‹ᴷarguments: LabeledExprListSyntax ‹ᴷ<0>: LabeledExprSyntax ‹ᴷexpression: MemberAccessExprSyntax ᵓᴷbase:
DeclReferenceExprSyntax │ ‹ᴷbaseName: identifier("SampleModel") ᵓᴷperiod: period ‹ᴷdeclName: DeclReferenceExprSyntax… node.arguments!.cast(LabeledExprListSyntax.self).first! .expression.as(MemberAccessExprSyntax.self)? .base?.as(DeclReferenceExprSyntax.self)? .baseName 6
"baseName"ͷऔಘྫ AttributeSyntax ᵓᴷ... ‹ᴷarguments: LabeledExprListSyntax ‹ᴷ<0>: LabeledExprSyntax ‹ᴷexpression: MemberAccessExprSyntax ᵓᴷbase:
DeclReferenceExprSyntax │ ‹ᴷbaseName: identifier("SampleModel") ᵓᴷperiod: period ‹ᴷdeclName: DeclReferenceExprSyntax… node.arguments!.cast(LabeledExprListSyntax.self).first! .expression.as(MemberAccessExprSyntax.self)? .base?.as(DeclReferenceExprSyntax.self)? .baseName 6
"baseName"ͷऔಘྫ AttributeSyntax ᵓᴷ... ‹ᴷarguments: LabeledExprListSyntax ‹ᴷ<0>: LabeledExprSyntax ‹ᴷexpression: MemberAccessExprSyntax ᵓᴷbase:
DeclReferenceExprSyntax │ ‹ᴷbaseName: identifier("SampleModel") ᵓᴷperiod: period ‹ᴷdeclName: DeclReferenceExprSyntax… node.arguments!.cast(LabeledExprListSyntax.self).first! .expression.as(MemberAccessExprSyntax.self)? .base?.as(DeclReferenceExprSyntax.self)? .baseName 6
lldbίϚϯυͰߏจΛදࣔ 6
ίʔυ͔ΒSyntaxͷΦϒδΣΫτม͢Δ ग़ྗ͍ͨ͠ίʔυͷߏจͷ༰͕Θ͔Εɺߏจʹهࡌ͞Ε͍ͯΔSyntaxͷܕͷΠχ γϟϥΠβͰࣗવͱSyntaxͷΦϒδΣΫτมͰ͖·͢ɻ SyntaxͷܕͷΠχγϟϥΠβͷύϥϝʔλ໊ɺߏจͷࢬͷཁૉ໊͕Ұக͍ͯ͠Δ͜ͱ ͕ɺ͜ͷํ๏Λ༰қʹ͍ͯ͠ΔϙΠϯτɻ 7
ExtensionDeclSyntaxͷੜྫ ߏจ ExtensionDeclSyntax ᵓᴷattributes: AttributeListSyntax ᵓᴷmodifiers: DeclModifierListSyntax ᵓᴷextensionKeyword: keyword(SwiftSyntax.Keyword.extension) ᵓᴷextendedType:
IdentifierTypeSyntax │ ‹ᴷname: identifier("SampleData") ᵓᴷinheritanceClause: InheritanceClauseSyntax │ ᵓᴷcolon: colon │ ‹ᴷinheritedTypes: InheritedTypeListSyntax │ ‹ᴷ<0>: InheritedTypeSyntax │ ‹ᴷtype: IdentifierTypeSyntax │ ‹ᴷname: identifier("ModelConvertible") ‹ᴷmemberBlock: MemberBlockSyntax ᵓᴷleftBrace: leftBrace ᵓᴷ... ... ߏจʹରԠ͢ΔΠχγϟϥΠβ ExtensionDeclSyntax( extensionKeyword: T##TokenSyntax, extendedType: T##TypeSyntaxProtocol, inheritanceClause: T##InheritanceClauseSyntax? memberBlock: T##MemberBlockSyntax ) 7
ExtensionDeclSyntaxͷੜྫ ߏจ ExtensionDeclSyntax ᵓᴷattributes: AttributeListSyntax ᵓᴷmodifiers: DeclModifierListSyntax ᵓᴷextensionKeyword: keyword(SwiftSyntax.Keyword.extension) ᵓᴷextendedType:
IdentifierTypeSyntax │ ‹ᴷname: identifier("SampleData") ᵓᴷinheritanceClause: InheritanceClauseSyntax │ ᵓᴷcolon: colon │ ‹ᴷinheritedTypes: InheritedTypeListSyntax │ ‹ᴷ<0>: InheritedTypeSyntax │ ‹ᴷtype: IdentifierTypeSyntax │ ‹ᴷname: identifier("ModelConvertible") ‹ᴷmemberBlock: MemberBlockSyntax ᵓᴷleftBrace: leftBrace ᵓᴷ... ... ߏจʹରԠ͢ΔΠχγϟϥΠβ ExtensionDeclSyntax( extensionKeyword: T##TokenSyntax, extendedType: T##TypeSyntaxProtocol, inheritanceClause: T##InheritanceClauseSyntax? memberBlock: T##MemberBlockSyntax ) 7
ExtensionDeclSyntaxͷੜྫ ߏจ ExtensionDeclSyntax ᵓᴷattributes: AttributeListSyntax ᵓᴷmodifiers: DeclModifierListSyntax ᵓᴷextensionKeyword: keyword(SwiftSyntax.Keyword.extension) ᵓᴷextendedType:
IdentifierTypeSyntax │ ‹ᴷname: identifier("SampleData") ᵓᴷinheritanceClause: InheritanceClauseSyntax │ ᵓᴷcolon: colon │ ‹ᴷinheritedTypes: InheritedTypeListSyntax │ ‹ᴷ<0>: InheritedTypeSyntax │ ‹ᴷtype: IdentifierTypeSyntax │ ‹ᴷname: identifier("ModelConvertible") ‹ᴷmemberBlock: MemberBlockSyntax ᵓᴷleftBrace: leftBrace ᵓᴷ... ... ߏจʹରԠ͢ΔΠχγϟϥΠβ ExtensionDeclSyntax( extensionKeyword: T##TokenSyntax, extendedType: T##TypeSyntaxProtocol, inheritanceClause: T##InheritanceClauseSyntax?, memberBlock: T##MemberBlockSyntax ) 7
ExtensionDeclSyntaxͷੜྫ ߏจ ExtensionDeclSyntax ᵓᴷattributes: AttributeListSyntax ᵓᴷmodifiers: DeclModifierListSyntax ᵓᴷextensionKeyword: keyword(SwiftSyntax.Keyword.extension) ᵓᴷextendedType:
IdentifierTypeSyntax │ ‹ᴷname: identifier("SampleData") ᵓᴷinheritanceClause: InheritanceClauseSyntax │ ᵓᴷcolon: colon │ ‹ᴷinheritedTypes: InheritedTypeListSyntax │ ‹ᴷ<0>: InheritedTypeSyntax │ ‹ᴷtype: IdentifierTypeSyntax │ ‹ᴷname: identifier("ModelConvertible") ‹ᴷmemberBlock: MemberBlockSyntax ᵓᴷleftBrace: leftBrace ᵓᴷ... ... ߏจʹରԠ͢ΔΠχγϟϥΠβ ExtensionDeclSyntax( extensionKeyword: T##TokenSyntax, extendedType: T##TypeSyntaxProtocol, inheritanceClause: T##InheritanceClauseSyntax?, memberBlock: T##MemberBlockSyntax ) 7
SwiftParserΛ͑ίʔυ͔ΒߏจΛऔಘͰ͖·͢ɻ var parser = Parser(""" extension SampleData: ModelConvertible { typealias
ModelType = SampleModel init(_ model: SampleModel) throws { id = model.id text = model.text } func toModel() throws -> SampleModel { return .init(id: id, text: text) } } """) let block = CodeBlockSyntax.parse(from: &parser) 7
7
ҙ ೖϋʔυϧΛԼ͛Δ͜ͱΛॏʹஔ͍͓ͯΓɺSwift macrosͷϕετϓϥΫςΟεͳ ࣮ํ๏ͱݶΓ·ͤΜɻ public struct StringifyMacro: ExpressionMacro { public
static func expansion( of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext ) -> ExprSyntax { ... return "(\(argument), \(literal: argument.description))" } } 8
·ͱΊ ߏจʹίʔυͱSwiftSyntaxͷ૬ޓม͢ΔͨΊͷใ͕٧·͍ͬͯΔ ߏจͷใΛ׆༻͢ΕɺSwift macrosͷϋʔυϧΛେ͖͘Լ͛ΒΕΔ . ͋͞ɺօ͞ΜϚΫϩΛ࡞ͬͯΈ·͠ΐ͏ʂ 9