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
Swiftのstructとイミュータビリティ
Search
Yuta Koshizawa
January 12, 2025
3
490
Swiftのstructとイミュータビリティ
Yuta Koshizawa
January 12, 2025
Tweet
Share
More Decks by Yuta Koshizawa
See All by Yuta Koshizawa
Swift 6のTyped throwsとSwiftにおけるエラーハンドリングの全体像を学ぶ
koher
4
3.4k
Swift Concurrency時代のiOSアプリの作り方
koher
15
8k
Swift Zoomin' #8
koher
2
580
async/awaitやactorでiOSアプリ開発がどう変わるか Before & Afterの具体例で学ぶ
koher
9
6.8k
Swift Language Updates - Learn Languages 2021
koher
8
1.7k
先取り! Swift 6 の async/await
koher
15
3.9k
SwiftUIで勘違いした話
koher
1
2.8k
iOSエンジニアのための、SwiftからPythonのライブラリを使って機械学習する方法 / Machine Learning using Python from Swift for iOS Engineer
koher
5
14k
Generalized existentialが内部的にすでに存在しているか調べてみた / A Dive to Find Inner Generalized Existential Containers
koher
2
320
Featured
See All Featured
4 Signs Your Business is Dying
shpigford
182
22k
VelocityConf: Rendering Performance Case Studies
addyosmani
327
24k
Fireside Chat
paigeccino
34
3.1k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Navigating Team Friction
lara
183
15k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3.1k
Building Adaptive Systems
keathley
38
2.4k
Statistics for Hackers
jakevdp
797
220k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Thoughts on Productivity
jonyablonski
68
4.4k
Making Projects Easy
brettharned
116
6k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Transcript
Swi$ͷ struct ͱ ΠϛϡʔλϏϦςΟ @koher
ࣗݾհ • @koher • ΤϯδχΞʢQonceptʣ • ΧϯϑΝϨϯεొஃ • try! Swi0
Tokyo: 2016 • iOSDC Japan: 2017, 2018, 2019, 2020, 2021, 2022, 2024 • Swi0 Zoomin' ओ࠵ • Heart of Swi0 ࣥච
࣭ struct Foo { var value: Int } ͜ͷ struct
ϛϡʔλϒϧͰ͔͢ʁΠϛϡʔλϒϧͰ͔͢ʁ
struct ϛϡʔλϒϧͰ ຊ࣭తʹΠϛϡʔλϒϧΫϥεͱಉ͡
var ϓϩύςΟΛ࣋ͭ struct struct Foo { var value: Int }
var ϓϩύςΟΛ࣋ͭ struct struct Foo { var value: Int }
extension Foo { mutating func increment() { value += 1 } }
var ϓϩύςΟΛ࣋ͭ struct var foo: Foo = .init(value: 0) foo.increment()
print(foo.value) // 1
let ϓϩύςΟΛ࣋ͭ struct struct Foo { let value: Int }
let ϓϩύςΟΛ࣋ͭ struct struct Foo { let value: Int }
extension Foo { mutating func increment() { value += 1 // ⛔ ίϯύΠϧΤϥʔ } }
let ϓϩύςΟΛ࣋ͭ struct struct Foo { let value: Int }
extension Foo { mutating func increment() { self = Foo(value: value + 1) // ͜ΕͳΒOK } }
ϝιουͱ // ϝιουͷ߹ extension Foo { // ҉ͷୈ1Ҿ self Λ࣋ͭ
func bar() -> Int { self.value * self.value } }
ϝιουͱ // ϝιουͷ߹ extension Foo { // ҉ͷୈ1Ҿ self Λ࣋ͭ
func bar() -> Int { self.value * self.value } } let foo: Foo = .init(value: 3) print(foo.bar()) // 9
ϝιουͱ // ؔͷ߹ // ໌ࣔతୈ1Ҿ self Λ࣋ͭ func bar(_ self:
Foo) -> Int { self.value * self.value } let foo: Foo = .init(value: 3) print(bar(foo)) // 9
mutating ͱ struct Foo { var value: Int } extension
Foo { mutating func increment() { self.value += 1 // ✅ } }
mutating ͱ struct Foo { var value: Int } extension
Foo { func increment() { self.value += 1 // ⛔ } }
mutating ͱ struct Foo { var value: Int } func
increment(_ self: Foo) { self.value += 1 // ⛔ }
mutating ͱ struct Foo { var value: Int } func
increment(_ self: inout Foo) { self.value += 1 // ✅ }
mutating ͱ struct Foo { var value: Int } extension
Foo { mutating func increment() { self.value += 1 // ✅ } }
let ϓϩύςΟΛ࣋ͭ struct struct Foo { let value: Int }
extension Foo { mutating func increment() { self = Foo(value: self.value + 1) // } }
let ϓϩύςΟΛ࣋ͭ struct struct Foo { let value: Int }
func increment(_ self: inout Foo) { self = Foo(value: self.value + 1) // }
let ϓϩύςΟΛ࣋ͭ struct struct Foo { let value: Int }
extension Foo { mutating func increment() { self = Foo(value: self.value + 1) // } }
let ϓϩύςΟΛ࣋ͭ struct var foo: Foo = .init(value: 0) foo.increment()
// ✅ Foo.value ͕ let Ͱ OK print(foo.value) // 1
ϓϩύςΟͷએݴ͕ var ͔ let ͔ struct ͷϛϡʔλϏϦςΟΛܾΊͳ͍
มʢఆʣએݴͷ var / let ͕ struct ͷϛϡʔλϏϦςΟΛܾΊΔ
มʢఆʣએݴͱϛϡʔλϏϦςΟ var foo: Foo = .init(value: 0) foo.increment() // ✅
print(foo.value)
มʢఆʣએݴͱϛϡʔλϏϦςΟ let foo: Foo = .init(value: 0) foo.increment() // ⛔
print(foo.value)
ܕͷϛϡʔλϏϦςΟΛ ࢀরܕͱಉ͡Α͏ʹߟ͍͚͑ͯͳ͍
ܕͷΠϯελϯε มͷͨΊʹ֬อ͞ΕͨϝϞϦྖҬͱҰମ
ΠϛϡʔλϒϧΫϥε final class Foo { let value: Int }
ΠϛϡʔλϒϧΫϥε final class Foo { let value: Int } extension
Foo { // ⛔ ೦ͳ͕Β͜ΕSwiftͷߏจͷͰͰ͖ͳ͍ mutating func increment() { self = Foo(value: self.value + 1) } }
ΠϛϡʔλϒϧΫϥε final class Foo { let value: Int } //
✅ ͜ΕͳΒOK func increment(_ self: inout Foo) { self = Foo(value: self.value + 1) }
ΠϛϡʔλϒϧΫϥε var foo: Foo = .init(value: 0) increment(&foo) // ✅
͜ΕͳΒ foo ΛมߋՄ print(foo.value) // 1
struct ͱΠϛϡʔλϒϧΫϥε ຊ࣭తʹಉ͜͡ͱ͕Ͱ͖Δ
ΠϛϡʔλϒϧΫϥεΠϯελϯεͷ ϝϞϦྖҬΛมߋͰ͖ͳ͍
ࢀরܕͰͳ͘ܕͷ ʢมଆͷʣϛϡʔλϏϦςΟͰߟ͑Ε ΠϛϡʔλϒϧΫϥε struct ͱಉ͡
ϛϡʔλϒϧΫϥε final class Foo { var value: Int }
ϛϡʔλϒϧΫϥε final class Foo { var value: Int } extension
Foo { func increment() { value += 1 } }
ϛϡʔλϒϧΫϥε let foo: Foo = .init(value: 0) foo.increment() print(foo.value) //
1
ϛϡʔλϒϧΫϥε let foo: Foo = .init(value: 0) foo.increment() print(foo.value) //
1
ϛϡʔλϒϧΫϥε let foo: Foo = .init(value: 0) let foo2 =
foo foo.increment() print(foo.value) // 1
ϛϡʔλϒϧΫϥε let foo: Foo = .init(value: 0) let foo2 =
foo foo.increment() print(foo.value) // 1 print(foo2.value) // 1
struct var foo: Foo = .init(value: 0) var foo2 =
foo foo.increment() print(foo.value) // 1 print(foo2.value) // 0
ΠϛϡʔλϒϧΫϥε var foo: Foo = .init(value: 0) var foo2 =
foo increment(&foo) print(foo.value) // 1 print(foo2.value) // 0
struct ͱΠϛϡʔλϒϧΫϥε shared mutable stateΛ࣋ͨͳ͍
Sendable ४ڌ final class User: Sendable { // var name:
String } final class User: Sendable { // let name: String } struct User: Sendable { // ✅ var name: String }
struct Λ͑ϛϡʔλϒϧͰ ΠϛϡʔλϒϧΫϥεಉ༷ͷརӹΛڗडՄೳ
·ͱΊ • struct ຊ࣭తʹΠϛϡʔλϒϧΫϥεͱಉ͡ • struct Λ͑ϛϡʔλϒϧͰΠϛϡʔλϒϧΫϥεͱಉ ͡རӹΛڗडͰ͖Δ