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
550
"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
プロダクト開発を支えるデータ利活用:中央集権から「民主化」までの軌跡
andpad
0
120
アンドパッドの Go 勉強会「 gopher 会」とその内容の紹介
andpad
0
350
読もう! Android build ドキュメント
andpad
1
430
アンドパッドにおける CocoaPods ライブラリ群の SwiftPackageManager への移行戦略
andpad
0
200
Flutter は DCM が 9 割
andpad
1
340
Amplify で SPA をホスティングする際の注意点
andpad
1
270
マルチプロダクト開発の現場でAWS Security Hubを1年以上運用して得た教訓
andpad
0
140
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
580
本編では話さない Zig の話
andpad
2
390
Other Decks in Programming
See All in Programming
11年かかって やっとVibe Codingに 時代が追いつきましたね
yimajo
1
260
React 使いじゃなくても知っておきたい教養としての React
oukayuka
18
5.6k
新しいモバイルアプリ勉強会(仮)について
uetyo
1
260
202507_ADKで始めるエージェント開発の基本 〜デモを通じて紹介〜(奥田りさ)The Basics of Agent Development with ADK — A Demo-Focused Introduction
risatube
PRO
6
1.4k
技術的負債で信頼性が限界だったWordPress運用をShifterで完全復活させた話
rvirus0817
1
1.6k
20250808_AIAgent勉強会_ClaudeCodeデータ分析の実運用〜競馬を題材に回収率100%の先を目指すメソッドとは〜
kkakeru
0
160
あなたとJIT, 今すぐアセンブ ル
sisshiki1969
1
640
令和最新版手のひらコンピュータ
koba789
13
7.6k
No Install CMS戦略 〜 5年先を見据えたフロントエンド開発を考える / no_install_cms
rdlabo
0
480
SwiftでMCPサーバーを作ろう!
giginet
PRO
2
240
[DevinMeetupTokyo2025] コード書かせないDevinの使い方
takumiyoshikawa
2
280
新世界の理解
koriym
0
130
Featured
See All Featured
The Power of CSS Pseudo Elements
geoffreycrofte
77
5.9k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
YesSQL, Process and Tooling at Scale
rocio
173
14k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.9k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
50
5.5k
Designing Experiences People Love
moore
142
24k
Code Reviewing Like a Champion
maltzj
524
40k
jQuery: Nuts, Bolts and Bling
dougneiner
64
7.8k
[RailsConf 2023] Rails as a piece of cake
palkan
56
5.8k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
131
19k
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 カジュアル⾯談 応募フォーム