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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
レガシー共有バッチ基盤への挑戦 - SREドリブンなリアーキテクチャリングの取り組み
tatsukoni
0
210
~Everything as Codeを諦めない~ 後からCDK
mu7889yoon
3
310
GitLab Duo Agent Platform × AGENTS.md で実現するSpec-Driven Development / GitLab Duo Agent Platform × AGENTS.md
n11sh1
0
130
10Xにおける品質保証活動の全体像と改善 #no_more_wait_for_test
nihonbuson
PRO
2
230
生成AI時代にこそ求められるSRE / SRE for Gen AI era
ymotongpoo
5
3k
Oracle Cloud Observability and Management Platform - OCI 運用監視サービス概要 -
oracle4engineer
PRO
2
14k
Introduction to Sansan for Engineers / エンジニア向け会社紹介
sansan33
PRO
6
68k
We Built for Predictability; The Workloads Didn’t Care
stahnma
0
140
会社紹介資料 / Sansan Company Profile
sansan33
PRO
15
400k
外部キー制約の知っておいて欲しいこと - RDBMSを正しく使うために必要なこと / FOREIGN KEY Night
soudai
PRO
12
5.2k
仕様書駆動AI開発の実践: Issue→Skill→PRテンプレで 再現性を作る
knishioka
2
620
データの整合性を保ちたいだけなんだ
shoheimitani
8
3.1k
Featured
See All Featured
Accessibility Awareness
sabderemane
0
51
WCS-LA-2024
lcolladotor
0
450
The untapped power of vector embeddings
frankvandijk
1
1.6k
Ruling the World: When Life Gets Gamed
codingconduct
0
140
RailsConf 2023
tenderlove
30
1.3k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
170
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
300
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Claude Code のすすめ
schroneko
67
210k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
580
Why Our Code Smells
bkeepers
PRO
340
58k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
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ͷΘΓʹݺΕΔ
ޚਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠