# How to programmatically detect if copy on write occurs

**URL:** <https://forums.swift.org/t/how-to-programmatically-detect-if-copy-on-write-occurs/85553>\
**Category:** Using Swift\
**Created:** [March 24, 2026, 7:30pm UTC](https://forums.swift.org/t/how-to-programmatically-detect-if-copy-on-write-occurs/85553 "2026-03-24T19:30:05Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![dnadoba](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dnadoba/32/11438_2.png) [@dnadoba](https://forums.swift.org/u/dnadoba)\
**Post date:** [March 27, 2026, 3:54pm UTC](https://forums.swift.org/t/how-to-programmatically-detect-if-copy-on-write-occurs/85553/9 "2026-03-27T15:54:51Z")

</div>

For this specific case you can use a couple techniques to get rid of CoW.  
If you make the type not a class but a struct you can consume it which should get rid of the intermediate copy:

```swift
struct C {
    enum State {
        case none
        case some([Int])
    }
    var state = State.some([])

    mutating func append(_ i: Int) {
        switch (consume self).state {
        case .none:
            self = .init(state: .some([i]))
        case .some(var value):
            value.append(i)
            self = .init(state: .some(value))
        }
    }
}

```

Copyable structs don't support partial consumption so you will need to consume self fully. A ~Copyable struct at least supports partial consumption but sadly doesn't yet support [reinitializing](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0429-partial-consumption.md#future-direction-partial-reinitialization) fields after they are consumed.  
It still improves it slightly:

```swift
struct C2: ~Copyable {
    enum State: ~Copyable {
        case none
        case some([Int])
    }
    var state = State.some([])

    mutating func append(_ i: Int) {
        switch consume state {
        case .none:
            self = .init(state: .some([i]))
        case .some(var value):
            value.append(i)
            self = .init(state: .some(value))
        }
    }
}

```

The real advantage of making everything ~Copyable is that we can now use `UniqueArray` (from [swift-collections](https://swiftpackageindex.com/apple/swift-collections/1.3.0/documentation/basiccontainers/uniquearray) today or [eventually in the standard library](https://forums.swift.org/t/pitch-rigidarray-and-uniquearray/85455)):

```swift
struct C3: ~Copyable {
    enum State: ~Copyable {
        case none
        case some(UniqueArray<Int>)
    }
    var state = State.some(.init())

    mutating func append(_ i: Int) {
        switch consume state {
        case .none:
            var array = UniqueArray<Int>()
            array.append(i)
            self = .init(state: .some(array))
        case .some(var value):
            value.append(i)
            self = .init(state: .some(value))
        }
    }
}

```

which actually grantees that we never trigger CoW as it doesn't even support it to begin with.

The `consume` of `state` and the reinitializing of the enum is still just a workaround for a [missing `inout` for pattern matching feature](https://forums.swift.org/t/pitch-borrow-and-inout-declaration-keywords/62366).

---

_[View the full topic](https://forums.swift.org/t/how-to-programmatically-detect-if-copy-on-write-occurs/85553)._
