[swift-users] isKnownUniquelyReferenced really a mutating function?

Andrew Trick atrick at apple.com
Wed Jan 25 12:39:38 CST 2017


> On Jan 25, 2017, at 10:20 AM, Edward Connell via swift-users <swift-users at swift.org> wrote:
> 
> I have a data structure that calls isKnownUniquelyReferenced on a member. It forces everything to be marked as mutating because of the inout parameter, however the parameter is never mutated, it is just read right?? The reason it is inout is because a read only reference is required.
> 
> If it is truly not mutating, is there some way around this so I don't have to mark everything in the caller chain as mutating also? It's kind of annoying...
> 
> Thanks, Ed

isKnownUniquelyReferenced doesn’t change the value of the reference. It has mutating semantics because that’s the only way to refer to the location holding the reference (as opposed to the reference’s value). It’s checking whether that location holds the only copy of that value.

You would normally only call isKnownUniquelyReferenced when you need to mutate the object, so I’ve never seen a usability issue. It would be interesting to see your case.

There have been proposals for future improvements to this API—isUniquelyReferenced could return a new reference of a different mutable type--but that hinges on future language support.

-Andy



More information about the swift-users mailing list