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
340
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
1.3k
Java 21 Overview
line_developers
PRO
6
770
Code Review Challenge: An example of a solution
line_developers
PRO
1
800
KARTEのAPIサーバ化
line_developers
PRO
1
360
著作権とは何か?〜初歩的概念から権利利用法、侵害要件まで
line_developers
PRO
5
1.7k
生成AIと著作権 〜生成AIによって生じる著作権関連の課題と対処
line_developers
PRO
3
1.7k
マイクロサービスにおけるBFFアーキテクチャでのモジュラモノリスの導入
line_developers
PRO
9
2.3k
A/B Testing at LINE NEWS
line_developers
PRO
2
590
LINEのサポートバージョンの考え方
line_developers
PRO
2
760
Other Decks in Technology
See All in Technology
「XX試験の環境作ってよ」と言われた時によく使うAWSのソリューションについて
bun913
0
120
やっていきテスト
k6s4i53rx
0
160
Cloud Friendly(?) Jenkins. How we failed to make Jenkins cloud native and what we learned?
onenashev
PRO
0
110
8週連続ウェビナー_イチから学ぶFivetran
cmsuzu
0
170
技育祭2024春 LT Finatextホールディングス
kevinrobot34
1
170
Autopsy of a Cascading Outage from a MySQL Crashing Bug
jfg956
0
200
技術広報として2023年度に頑張ったこと / What we did well in FY2023 as a DevRel
pauli
5
490
Vos logs méritent mieux que la config par défaut
lyrixx
2
350
今さら聞けない!? AWSの生成AIサービス Amazon Bedrock入門!
minorun365
PRO
11
2.6k
OCI Data Integration技術情報 / ocidi_technical_jp
oracle4engineer
PRO
1
1.5k
VSCode上からSlackにメッセージを送る拡張機能を作っている話
ebarakazuhiro
0
120
エバンジェリスト活動を7年やってきて見えてきた、コミュニティとエバンジェリストの関係
soracom
PRO
1
200
Featured
See All Featured
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
319
20k
Testing 201, or: Great Expectations
jmmastey
27
6.3k
Build your cross-platform service in a week with App Engine
jlugia
223
17k
GitHub's CSS Performance
jonrohan
1023
450k
Building Flexible Design Systems
yeseniaperezcruz
317
37k
How GitHub Uses GitHub to Build GitHub
holman
467
290k
Statistics for Hackers
jakevdp
789
220k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
12
1.4k
For a Future-Friendly Web
brad_frost
170
8.9k
Clear Off the Table
cherdarchuk
82
310k
Code Review Best Practice
trishagee
54
15k
Debugging Ruby Performance
tmm1
68
11k
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ͷΘΓʹݺΕΔ
ޚਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠