Consume operator in Swift 5.9

Pawan Manjani
2 min readJan 8, 2024

--

In Swift 5.9 we got new operator consume which helps us to handle Copy On Write (COW) data structures and reduce retain/release calls.

Suppose you have function which you are passing the local variable to another function then again the next function is passing that variable to another function and so on… this will not allow to release first func variable until it is being used somewhere with any function

So here consume will help us here

Gif Source: https://giphy.com

In above code if we are trying to access the arry after consume then compiler will not allow us to do.

Now compiler is happy because we are using new variable newArr variable

But we can reinitialize the variable and use it again.

What if we use consume with global variable????

Compiler will throw error that we can use only consume with local variables.

For more detail
https://github.com/apple/swift-evolution/blob/main/proposals/0366-move-function.md

Please like and follow for more

Thanks for reading, Happy Coding 💻

--

--

Responses (1)