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
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
21
2.5k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Rebuilding a faster, lazier Slack
samanthasiow
80
8.8k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
133
33k
Optimising Largest Contentful Paint
csswizardry
34
3.1k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.3k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
Designing Experiences People Love
moore
140
23k
Producing Creativity
orderedlist
PRO
344
39k
How to Think Like a Performance Engineer
csswizardry
22
1.3k
Product Roadmaps are Hard
iamctodd
PRO
50
11k
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 Λ͑ϛϡʔλϒϧͰΠϛϡʔλϒϧΫϥεͱಉ ͡རӹΛڗडͰ͖Δ