Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swiftのstructとイミュータビリティ

Yuta Koshizawa
January 12, 2025
490

 Swiftのstructとイミュータビリティ

Yuta Koshizawa

January 12, 2025
Tweet

Transcript

  1. ࣗݾ঺հ • @koher • ΤϯδχΞʢQonceptʣ • ΧϯϑΝϨϯεొஃ • try! Swi0

    Tokyo: 2016 • iOSDC Japan: 2017, 2018, 2019, 2020, 2021, 2022, 2024 • Swi0 Zoomin' ओ࠵ • Heart of Swi0 ࣥච
  2. ࣭໰ struct Foo { var value: Int } ͜ͷ struct

    ͸ϛϡʔλϒϧͰ͔͢ʁΠϛϡʔλϒϧͰ͔͢ʁ
  3. var ϓϩύςΟΛ࣋ͭ struct struct Foo { var value: Int }

    extension Foo { mutating func increment() { value += 1 } }
  4. let ϓϩύςΟΛ࣋ͭ struct struct Foo { let value: Int }

    extension Foo { mutating func increment() { value += 1 // ⛔ ίϯύΠϧΤϥʔ } }
  5. let ϓϩύςΟΛ࣋ͭ struct struct Foo { let value: Int }

    extension Foo { mutating func increment() { self = Foo(value: value + 1) // ͜ΕͳΒOK } }
  6. ϝιουͱ͸ // ϝιουͷ৔߹ extension Foo { // ҉໧ͷୈ1Ҿ਺ self Λ࣋ͭ

    func bar() -> Int { self.value * self.value } } let foo: Foo = .init(value: 3) print(foo.bar()) // 9
  7. ϝιουͱ͸ // ؔ਺ͷ৔߹ // ໌ࣔతୈ1Ҿ਺ self Λ࣋ͭ func bar(_ self:

    Foo) -> Int { self.value * self.value } let foo: Foo = .init(value: 3) print(bar(foo)) // 9
  8. mutating ͱ͸ struct Foo { var value: Int } extension

    Foo { mutating func increment() { self.value += 1 // ✅ } }
  9. mutating ͱ͸ struct Foo { var value: Int } extension

    Foo { func increment() { self.value += 1 // ⛔ } }
  10. mutating ͱ͸ struct Foo { var value: Int } func

    increment(_ self: Foo) { self.value += 1 // ⛔ }
  11. mutating ͱ͸ struct Foo { var value: Int } func

    increment(_ self: inout Foo) { self.value += 1 // ✅ }
  12. mutating ͱ͸ struct Foo { var value: Int } extension

    Foo { mutating func increment() { self.value += 1 // ✅ } }
  13. let ϓϩύςΟΛ࣋ͭ struct struct Foo { let value: Int }

    extension Foo { mutating func increment() { self = Foo(value: self.value + 1) // } }
  14. let ϓϩύςΟΛ࣋ͭ struct struct Foo { let value: Int }

    func increment(_ self: inout Foo) { self = Foo(value: self.value + 1) // }
  15. let ϓϩύςΟΛ࣋ͭ struct struct Foo { let value: Int }

    extension Foo { mutating func increment() { self = Foo(value: self.value + 1) // } }
  16. let ϓϩύςΟΛ࣋ͭ struct var foo: Foo = .init(value: 0) foo.increment()

    // ✅ Foo.value ͕ let Ͱ΋ OK print(foo.value) // 1
  17. ΠϛϡʔλϒϧΫϥε final class Foo { let value: Int } extension

    Foo { // ⛔ ࢒೦ͳ͕Β͜Ε͸Swiftͷߏจͷ໰୊ͰͰ͖ͳ͍ mutating func increment() { self = Foo(value: self.value + 1) } }
  18. ΠϛϡʔλϒϧΫϥε final class Foo { let value: Int } //

    ✅ ͜ΕͳΒOK func increment(_ self: inout Foo) { self = Foo(value: self.value + 1) }
  19. ϛϡʔλϒϧΫϥε final class Foo { var value: Int } extension

    Foo { func increment() { value += 1 } }
  20. ϛϡʔλϒϧΫϥε let foo: Foo = .init(value: 0) let foo2 =

    foo foo.increment() print(foo.value) // 1
  21. ϛϡʔλϒϧΫϥε let foo: Foo = .init(value: 0) let foo2 =

    foo foo.increment() print(foo.value) // 1 print(foo2.value) // 1
  22. struct var foo: Foo = .init(value: 0) var foo2 =

    foo foo.increment() print(foo.value) // 1 print(foo2.value) // 0
  23. ΠϛϡʔλϒϧΫϥε var foo: Foo = .init(value: 0) var foo2 =

    foo increment(&foo) print(foo.value) // 1 print(foo2.value) // 0
  24. Sendable ४ڌ final class User: Sendable { // var name:

    String } final class User: Sendable { // let name: String } struct User: Sendable { // ✅ var name: String }