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
520
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.5k
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
What's in a price? How to price your products and services
michaelherold
244
12k
Visualization
eitanlees
146
15k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
27
1.5k
Mobile First: as difficult as doing things right
swwweet
222
9k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
240
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
3
360
Making Projects Easy
brettharned
116
6k
Producing Creativity
orderedlist
PRO
343
39k
Building Flexible Design Systems
yeseniaperezcruz
328
38k
Building Adaptive Systems
keathley
38
2.4k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.4k
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 Λ͑ϛϡʔλϒϧͰΠϛϡʔλϒϧΫϥεͱಉ ͡རӹΛڗडͰ͖Δ