Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 4.2 - __shared attribute near type

Tags:

swift

https://developer.apple.com/documentation/swift/double?changes=latest_minor In updated version of class definition I found init method for Double with NSNumber near type the unknown keyword __shared. What does it mean?

like image 335
Slava Gorloff Avatar asked Jan 25 '19 05:01

Slava Gorloff


1 Answers

The __shared parameter annotation means that a value type parameter can be passed by reference.

For value types, this enables us to elide a copy before we make the call and instead pass a reference pointing right at the memory we allocated. SIL calls this convention in_guaranteed for (indirect reference with guaranteed lifetime). It's currently the way we pass self in non-mutating functions.

It was introduced by the Ownership Manifesto.

Here is the difference between the ownership annotations:

  • inout: mutating pointer-like value
  • __shared: non-mutating pointer-like value
  • __owned: an explicit way of writing the default

Here is a summary of the manifesto: Swift Ownership Manifesto TL;DR.

like image 172
Yannick Loriot Avatar answered Nov 19 '22 22:11

Yannick Loriot