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
"noncopyable types" の使いどころについて考えてみた
Search
ANDPAD inc
September 26, 2024
Programming
0
560
"noncopyable types" の使いどころについて考えてみた
栗山 徹
@kotetu
2024 年 9 月 6 日
iOSDC 2024 Effect presented by Sansan & ANDPAD
ANDPAD inc
September 26, 2024
Tweet
Share
More Decks by ANDPAD inc
See All by ANDPAD inc
OSS開発者という働き方
andpad
5
1.7k
Vue・React マルチプロダクト開発を支える Vite
andpad
0
110
プロダクト開発を支えるデータ利活用:中央集権から「民主化」までの軌跡
andpad
0
140
アンドパッドの Go 勉強会「 gopher 会」とその内容の紹介
andpad
0
380
読もう! Android build ドキュメント
andpad
1
440
アンドパッドにおける CocoaPods ライブラリ群の SwiftPackageManager への移行戦略
andpad
0
210
Flutter は DCM が 9 割
andpad
1
350
Amplify で SPA をホスティングする際の注意点
andpad
1
300
マルチプロダクト開発の現場でAWS Security Hubを1年以上運用して得た教訓
andpad
0
140
Other Decks in Programming
See All in Programming
Testing Trophyは叫ばない
toms74209200
0
880
Oracle Database Technology Night 92 Database Connection control FAN-AC
oracle4engineer
PRO
1
450
FindyにおけるTakumi活用と脆弱性管理のこれから
rvirus0817
0
520
詳解!defer panic recover のしくみ / Understanding defer, panic, and recover
convto
0
240
1から理解するWeb Push
dora1998
7
1.9k
AWS発のAIエディタKiroを使ってみた
iriikeita
1
190
意外と簡単!?フロントエンドでパスキー認証を実現する WebAuthn
teamlab
PRO
2
760
概念モデル→論理モデルで気をつけていること
sunnyone
2
270
プロパティベーステストによるUIテスト: LLMによるプロパティ定義生成でエッジケースを捉える
tetta_pdnt
0
1.7k
JSONataを使ってみよう Step Functionsが楽しくなる実践テクニック #devio2025
dafujii
1
530
AIと私たちの学習の変化を考える - Claude Codeの学習モードを例に
azukiazusa1
10
4.3k
「待たせ上手」なスケルトンスクリーン、 そのUXの裏側
teamlab
PRO
0
530
Featured
See All Featured
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
8
530
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
112
20k
Rails Girls Zürich Keynote
gr2m
95
14k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
126
53k
It's Worth the Effort
3n
187
28k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
131
19k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Building Adaptive Systems
keathley
43
2.7k
Testing 201, or: Great Expectations
jmmastey
45
7.7k
Optimizing for Happiness
mojombo
379
70k
Transcript
© 2024 ANDPAD All Rights Reserved. "noncopyable types" の使いどころについて考えてみた iOSDC
2024 Effect presented by Sansan & ANDPAD 1
© 2024 ANDPAD All Rights Reserved. Confidential Swift 5.9 で導入された
“noncopyable types” を紹介しつつ、 どのような場面で使えるのかについて考えてみた。 セッションのテーマ 2
© 2024 ANDPAD All Rights Reserved. Confidential 2022年6月入社。 2023年6月より黒板機能 (iOS)
の開発を担当。 iOSDC Japan 2024 「ANDPAD黒板のオフラインモード機能 リリースまでの軌跡」 iOSDC Japan 2023 「認証体験向上のために passkeys (パスキー) に対応する」 「Swift Markdownを使ったMarkdownアプリ開発」 栗山 徹 開発本部 Tech Lead Profile | 経 歴 自己紹介 3
© 2024 ANDPAD All Rights Reserved. Confidential noncopyable types とは何か?
利用すると良さそうなケースについて考えてみた まとめ 1 2 3 アジェンダ 4
© 2024 ANDPAD All Rights Reserved. Confidential 現場の効率化から経営改善まで一元管理できる クラウド型建設プロジェクト管理サービス 社 内
社 外 営業 / 監督 / 設計 事務 / 管理職 職人 / 業者 メーカー / 流通 案件管理 資料 工程表 写真 報告 チャット 黒板 図面 受発注 • • • ANDPAD とは 5
© 2024 ANDPAD All Rights Reserved. noncopyable types とは何か? 01
6
© 2024 ANDPAD All Rights Reserved. Confidential 7 (無理矢理1行で説明すると) 値型のコピーに関する制約の追加
noncopyable types •WWDC 2024 - Consume noncopyable types in Swift https://developer.apple.com/videos/play/wwdc2024/10170/ •swift-evolution - SE-0390 (Noncopyable structs and enums) https://github.com/swiftlang/swift-evolution/blob/main/proposals/0390-noncopyable-structs-and-enums.md •Apple Developer の Copyable のドキュメント https://developer.apple.com/documentation/Swift/Copyable 一次情報
© 2024 ANDPAD All Rights Reserved. Confidential 8 代入時の挙動やメモリ管理方式が異なる Swift
の値型と参照型|値型 •struct, enum •代入時は別なインスタンスを生成 (値はコピー) •参照カウントが無い •スタック領域に格納される Swift の値型 file2 file1
© 2024 ANDPAD All Rights Reserved. Confidential 9 代入時の挙動やメモリ管理方式が異なる Swift
の値型と参照型|参照型 •class •代入時はインスタンスの参照がコピーされる •参照カウント (カウント0になったら解放される) •ヒープ領域に格納される Swift の参照型 File インスタンス file1 (参照) file2 (参照)
© 2024 ANDPAD All Rights Reserved. Confidential 10 参照型のように一意のデータを表現するのが難しい Swift
の値型の課題 File インスタンス file1 (参照) file2 (参照) file2 file1 値型 参照型 •値型の代入は全てコピー •参照型を使うと参照カウントの管理が煩雑 •ヒープ領域に格納されるので参照カウント管理と併せてオーバーヘッドが生じる
© 2024 ANDPAD All Rights Reserved. Confidential 11 値型のコピーを Copyable
と ~Copyable で制御 noncopyable types の概要 Copyable と ~Copyable (noncopyable) •これまでの値型 (コピー自由) は Copyable に準拠する型と再定義 •通常の値型は暗黙的に Copyable に準拠する (明示的につけなくても準拠) •Copyable に適合しない型を ~Copyable に準拠した値型として定義 •Copyable と ~Copyable は包含関係にある
© 2024 ANDPAD All Rights Reserved. Confidential 12 noncopyable types
の概要|Copyable https://developer.apple.com/videos/play/wwdc2024/10170/ より引用
© 2024 ANDPAD All Rights Reserved. Confidential 13 noncopyable types
の概要|~Copyable https://developer.apple.com/videos/play/wwdc2024/10170/ より引用
© 2024 ANDPAD All Rights Reserved. Confidential 14 代入時の挙動が変わる ~Copyable
に準拠するとどうなるのか|Copyable Copyable に準拠した値型
© 2024 ANDPAD All Rights Reserved. Confidential 15 代入時の挙動が変わる ~Copyable
に準拠するとどうなるのか|~Copyable ~Copyable に準拠した値型
© 2024 ANDPAD All Rights Reserved. Confidential 16 所有権 (ownership)
が移動したため (所有権が無くなった後の参照・代入はできない) なぜエラーとなるのか? user1 代入時点 user1 所有権 user2 user2 代入時点 user1 所有権 user2 user1 は参照できない
© 2024 ANDPAD All Rights Reserved. Confidential 17 別な変数への代入に使用した変数は使用できなくなる →所有権を通じて一意性が生じている
~Copyable に準拠するとどうなるのか ~Copyable に準拠した値型 user1 user2
© 2024 ANDPAD All Rights Reserved. Confidential 18 メソッドの引数に下記3つのいずれかを指定する必要がある 3
つの ownership 指定 ownership 指定 •consuming : 所有権の移動が発生する、参照・変更可能 •borrowing : 所有権の移動が発生しない、参照のみ •inout : 所有権の移動が発生しない、参照・変更可能 (パラメータの再初期化が必要)
© 2024 ANDPAD All Rights Reserved. 利用すると良さそうなケースについて考えてみた 02 19
© 2024 ANDPAD All Rights Reserved. Confidential 20 ファイル関係など、低レイヤでの利用例が多い noncopyable
types のドキュメント・発表における利用事例 WWDC の場合 •取引情報の処理 •フロッピーディスクの処理 swift-evolution の場合 •ファイルディスクリプタといったファイル関連処理
© 2024 ANDPAD All Rights Reserved. Confidential 21 参照型で一意性を担保する必要がある箇所については 今後値型で実現できるようになる?
今後活用が見込まれそうな用途 class の方が実装しやすい (?) 箇所 •API Client ? •ファイル読み書き? ~Copyable がアプリ開発で必須と言われるためには もう少し活用方法の模索が必要
© 2024 ANDPAD All Rights Reserved. まとめ 03 22
© 2024 ANDPAD All Rights Reserved. Confidential noncopyable types は、値型のコピーに制約をつけることで
値型に新たな使い方をもたらした 既存のプロダクトコードに導入するには、まだ試行錯誤が必要 1 2 セッションのまとめ 23
© 2024 ANDPAD All Rights Reserved. We are Hiring! 開発部公式
X アカウント @andpad_dev カジュアル⾯談 応募フォーム