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
600
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.8k
Swift Concurrency時代のiOSアプリの作り方
koher
15
8.1k
Swift Zoomin' #8
koher
2
590
async/awaitやactorでiOSアプリ開発がどう変わるか Before & Afterの具体例で学ぶ
koher
9
6.9k
Swift Language Updates - Learn Languages 2021
koher
8
1.7k
先取り! Swift 6 の async/await
koher
15
4k
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
330
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1030
460k
Typedesign – Prime Four
hannesfritz
40
2.5k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
Done Done
chrislema
182
16k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Site-Speed That Sticks
csswizardry
4
390
Scaling GitHub
holman
459
140k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
4
350
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.2k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
410
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.5k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
175
52k
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 Λ͑ϛϡʔλϒϧͰΠϛϡʔλϒϧΫϥεͱಉ ͡རӹΛڗडͰ͖Δ