Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Function Builders in Swift 5.1
Search
LINE Developers
June 25, 2019
Technology
1
370
Function Builders in Swift 5.1
Christopher Rogers
WWDC19 Recap at LINE
https://line.connpass.com/event/134346/
LINE Developers
June 25, 2019
Tweet
Share
More Decks by LINE Developers
See All by LINE Developers
LINEスタンプのSREing事例集:大きなスパイクアクセスを捌くためのSREing
line_developers
3
2.4k
Java 21 Overview
line_developers
6
1.3k
Code Review Challenge: An example of a solution
line_developers
1
1.5k
KARTEのAPIサーバ化
line_developers
1
600
著作権とは何か?〜初歩的概念から権利利用法、侵害要件まで
line_developers
5
2.3k
生成AIと著作権 〜生成AIによって生じる著作権関連の課題と対処
line_developers
3
2.3k
マイクロサービスにおけるBFFアーキテクチャでのモジュラモノリスの導入
line_developers
9
3.8k
A/B Testing at LINE NEWS
line_developers
3
1.1k
LINEのサポートバージョンの考え方
line_developers
2
1.4k
Other Decks in Technology
See All in Technology
[CV勉強会@関東 World Model 読み会] Orbis: Overcoming Challenges of Long-Horizon Prediction in Driving World Models (Mousakhan+, NeurIPS 2025)
abemii
0
110
Azure Durable Functions で作った NL2SQL Agent の精度向上に取り組んだ話/jat08
thara0402
0
170
今日から始めるAmazon Bedrock AgentCore
har1101
4
400
クレジットカード決済基盤を支えるSRE - 厳格な監査とSRE運用の両立 (SRE Kaigi 2026)
capytan
6
2.7k
usermode linux without MMU - fosdem2026 kernel devroom
thehajime
0
230
超初心者からでも大丈夫!オープンソース半導体の楽しみ方〜今こそ!オレオレチップをつくろう〜
keropiyo
0
110
Introduction to Sansan, inc / Sansan Global Development Center, Inc.
sansan33
PRO
0
3k
Introduction to Sansan for Engineers / エンジニア向け会社紹介
sansan33
PRO
6
68k
FinTech SREのAWSサービス活用/Leveraging AWS Services in FinTech SRE
maaaato
0
130
データの整合性を保ちたいだけなんだ
shoheimitani
8
3.1k
Contract One Engineering Unit 紹介資料
sansan33
PRO
0
13k
SREじゃなかった僕らがenablingを通じて「SRE実践者」になるまでのリアル / SRE Kaigi 2026
aeonpeople
6
2.2k
Featured
See All Featured
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.9k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
57
Design in an AI World
tapps
0
140
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
290
How GitHub (no longer) Works
holman
316
140k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
130
Scaling GitHub
holman
464
140k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
180
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
910
Abbi's Birthday
coloredviolet
1
4.7k
Transcript
Function Builders in Swift 5.1 Christopher Rogers Twitter: @christorogers /
GitHub: @ChristopherRogers
Function Builder ͱʁ ؔΛϏϧμʔߏจʹมߋͰ͖Δ৽ػೳ1 • Embedded Domain-Specific Language (eDSL) •
SwiftUI ͷͨΊʁ • GM·ͰมΘΔՄೳੑ͕ߴ͍ 1 https://forums.swift.org/t/function-builders/25167
var body: some View { HStack { Image(uiImage: profile.image) Spacer()
Text(profile.name) } }
var body: some View { /* return লུ */ HStack
{ // ͔͜͜Β͕ function builder ߏจ Image(uiImage: profile.image) Spacer() Text(profile.name) } } public HStack { public init(@ViewBuilder _ content: () -> Content) }
var body: some View { /* return লུ */ HStack
{ // ͔͜͜Β͕ function builder ߏจ Image(uiImage: profile.image) Spacer() Text(profile.name) } }
var body: some View { /* return লུ */ HStack
{ // ͔͜͜Β͕ function builder ߏจ let _v0 = Image(uiImage: profile.image) let _v1 = Spacer() let _v2 = Text(profile.name) return ViewBuilder.buildBlock(_v0, _v1, _v2) } }
• ܕΛఆٛ͢Δ • ܕͷએݴΛ @_functionBuilder Ͱम০͢Δ • ಛఆͷϝιουΛ࣮͢Δ • ४ڌ͢Δ
protocol ͳ͍ • ʢݱ࣌Ͱʣϝιουͯ͢ static • ҾͱฦΓͷܕ͋Δఔࣗ༝ • ਂ͞༏ઌ୳ࡧͰࣜΛධՁͯ͠ม͍ͯ͘͠ @_functionBuilder public struct ViewBuilder { public static func buildBlock<Content>(_ content: Content) -> Content where Content: View }
buildBlock • །Ұඞਢͷϝιου • จʢ{}Λͬͨͱ͜Ζʣ୯ҐͰม͢Δ • ͯ͢ͷࣜΛ·ͱΊͯҾͱͯ͠ड͚औΔ • ՄมҾ •
ϝιου overload Ͱෳ࣮ let _v0 = Image(uiImage: profile.image) let _v1 = Spacer() let _v2 = Text(profile.name) return ViewBuilder.buildBlock(_v0, _v1, _v2)
buildExpression • ࣜ୯ҐͰม͢Δ͜ͱ͕Մೳ • ݱࡏར༻ෆՄ let _v0 = ViewBuilder.buildExpression(Image(uiImage: profile.image))
let _v1 = ViewBuilder.buildExpression(Spacer()) let _v2 = ViewBuilder.buildExpression(Text(profile.name)) return ViewBuilder.buildBlock(_v0, _v1, _v2)
buildOptional • ࣮͢Ε if จ͕͑ΔΑ͏ʹͳΔ • Ҿ Optional ܕʹ͢Δඞཁ͕͋Δ •
ϝιου໊ʹ͍ͭͯٞத • beta 2 Ͱڍಈ͕༷ͱҟͳΔ
if isEnabled { "hoge" } else { 42 }
var _v0Opt: String? var _v1Opt: Int? if isEnabled { _v0
= "hoge" } else { _v1 = 42 } let _v0 = MyBuilder.buildOptional(_v0Opt) let _v1 = MyBuilder.buildOptional(_v1Opt)
buildEither • ݅ʹΑΒͣܕΛ֬ఆ͢Δ͜ͱ͕Ͱ͖Δ • first/second ύϥϝʔλϥϕϧͰذ͕ද͞ΕΔ • ݱࡏར༻ෆՄ let _v0:
String if languageCode == "ja" { _v0 = MyBuilder.buildEither(first: "hoge") } else if languageCode == "en" { _v0 = MyBuilder.buildEither(second: MyBuilder.buildEither(first: "foo")) } else { _v0 = MyBuilder.buildEither(second: MyBuilder.buildEither(second: "")) }
ͦͷଞͷϝιου buildDo • do จͰಛఆͷڍಈΛಋೖ͢Δ߹ʹ༻͍Δ • buildBlockͷΘΓʹݺΕΔ buildFunction • ฦΓͷܕΛ࠷ޙʹม͢Δ߹ʹ༻͍Δ
• Ұ൪࠷ޙʹbuildBlockͷΘΓʹݺΕΔ
ޚਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠