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
590
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
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.4k
Facilitating Awesome Meetings
lara
52
6.2k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Faster Mobile Websites
deanohume
306
31k
A better future with KSS
kneath
238
17k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
Become a Pro
speakerdeck
PRO
26
5.1k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
30
4.6k
Building an army of robots
kneath
303
45k
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 Λ͑ϛϡʔλϒϧͰΠϛϡʔλϒϧΫϥεͱಉ ͡རӹΛڗडͰ͖Δ