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
PRO
June 25, 2019
Technology
1
360
Function Builders in Swift 5.1
Christopher Rogers
WWDC19 Recap at LINE
https://line.connpass.com/event/134346/
LINE Developers
PRO
June 25, 2019
Tweet
Share
More Decks by LINE Developers
See All by LINE Developers
LINEスタンプのSREing事例集:大きなスパイクアクセスを捌くためのSREing
line_developers
PRO
1
2k
Java 21 Overview
line_developers
PRO
6
1k
Code Review Challenge: An example of a solution
line_developers
PRO
1
1.1k
KARTEのAPIサーバ化
line_developers
PRO
1
440
著作権とは何か?〜初歩的概念から権利利用法、侵害要件まで
line_developers
PRO
5
2k
生成AIと著作権 〜生成AIによって生じる著作権関連の課題と対処
line_developers
PRO
3
2k
マイクロサービスにおけるBFFアーキテクチャでのモジュラモノリスの導入
line_developers
PRO
9
3k
A/B Testing at LINE NEWS
line_developers
PRO
3
830
LINEのサポートバージョンの考え方
line_developers
PRO
2
1.1k
Other Decks in Technology
See All in Technology
Terraform Stacks入門 #HashiTalks
msato
0
360
飲食店データの分析事例とそれを支えるデータ基盤
kimujun
0
210
FlutterアプリにおけるSLI/SLOを用いたユーザー体験の可視化と計測基盤構築
ostk0069
0
120
Exadata Database Service on Dedicated Infrastructure(ExaDB-D) UI スクリーン・キャプチャ集
oracle4engineer
PRO
2
3.2k
TypeScriptの次なる大進化なるか!? 条件型を返り値とする関数の型推論
uhyo
2
1.7k
SSMRunbook作成の勘所_20241120
koichiotomo
3
170
OS 標準のデザインシステムを超えて - より柔軟な Flutter テーマ管理 | FlutterKaigi 2024
ronnnnn
1
300
CDCL による厳密解法を採用した MILP ソルバー
imai448
3
180
OCI 運用監視サービス 概要
oracle4engineer
PRO
0
4.8k
個人でもIAM Identity Centerを使おう!(アクセス管理編)
ryder472
4
240
iOS/Androidで同じUI体験をネ イティブで作成する際に気をつ けたい落とし穴
fumiyasac0921
1
110
組織成長を加速させるオンボーディングの取り組み
sudoakiy
2
220
Featured
See All Featured
Facilitating Awesome Meetings
lara
50
6.1k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Designing for Performance
lara
604
68k
Testing 201, or: Great Expectations
jmmastey
38
7.1k
Why Our Code Smells
bkeepers
PRO
334
57k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
The Cult of Friendly URLs
andyhume
78
6k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
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ͷΘΓʹݺΕΔ
ޚਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠