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
1
2.3k
Java 21 Overview
line_developers
6
1.2k
Code Review Challenge: An example of a solution
line_developers
1
1.3k
KARTEのAPIサーバ化
line_developers
1
530
著作権とは何か?〜初歩的概念から権利利用法、侵害要件まで
line_developers
5
2.1k
生成AIと著作権 〜生成AIによって生じる著作権関連の課題と対処
line_developers
3
2.1k
マイクロサービスにおけるBFFアーキテクチャでのモジュラモノリスの導入
line_developers
9
3.5k
A/B Testing at LINE NEWS
line_developers
3
970
LINEのサポートバージョンの考え方
line_developers
2
1.3k
Other Decks in Technology
See All in Technology
あなたの声を届けよう! 女性エンジニア登壇の意義とアウトプット実践ガイド #wttjp / Call for Your Voice
kondoyuko
4
440
【TiDB GAME DAY 2025】Shadowverse: Worlds Beyond にみる TiDB 活用術
cygames
0
1.1k
AIのAIによるAIのための出力評価と改善
chocoyama
2
550
PostgreSQL 18 cancel request key長の変更とRailsへの関連
yahonda
0
120
GitHub Copilot の概要
tomokusaba
1
130
Yamla: Rustでつくるリアルタイム性を追求した機械学習基盤 / Yamla: A Rust-Based Machine Learning Platform Pursuing Real-Time Capabilities
lycorptech_jp
PRO
3
120
Prox Industries株式会社 会社紹介資料
proxindustries
0
290
Model Mondays S2E02: Model Context Protocol
nitya
0
220
変化する開発、進化する体系時代に適応するソフトウェアエンジニアの知識と考え方(JaSST'25 Kansai)
mizunori
1
210
「Chatwork」の認証基盤の移行とログ活用によるプロダクト改善
kubell_hr
1
150
生成AI時代の開発組織・技術・プロセス 〜 ログラスの挑戦と考察 〜
itohiro73
0
150
Amazon Bedrockで実現する 新たな学習体験
kzkmaeda
2
540
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
134
9.3k
Building Flexible Design Systems
yeseniaperezcruz
328
39k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
What's in a price? How to price your products and services
michaelherold
246
12k
Build The Right Thing And Hit Your Dates
maggiecrowley
36
2.8k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
VelocityConf: Rendering Performance Case Studies
addyosmani
330
24k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
20
1.3k
Producing Creativity
orderedlist
PRO
346
40k
For a Future-Friendly Web
brad_frost
179
9.8k
Designing for humans not robots
tammielis
253
25k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
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ͷΘΓʹݺΕΔ
ޚਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠