[swift-evolution] When to use argument labels, part DEUX (was: when to use argument labels (a new approach))

Charles Srstka cocoadev at charlessoft.com
Sat Feb 6 19:37:49 CST 2016


> On Feb 6, 2016, at 2:47 PM, Jean-Daniel Dupas via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Wouldn’t it be possible to simply drop the copyWithZone: method that is deprecated for some times now (To quote the doc: Zones are ignored on iOS and 64-bit runtime on OS X. You should not use zones in current development).

Unfortunately, the frameworks often call the copyWithZone() method directly on objects that support NSCopying, rather than copy(). For example, @property (copy) properties in Objective-C, and @NSCopying properties in Swift behave this way:

--

import Foundation

class Foo: NSObject, NSCopying {
	override func copy() -> AnyObject {
		print("copy() called")
		return Foo()
	}
	
	func copyWithZone(zone: NSZone) -> AnyObject {
		print("copyWithZone() called")
		return Foo()
	}
}

class Bar {
	@NSCopying var foo: Foo = Foo()
}

let bar = Bar()

bar.foo = Foo()

outputs:

copyWithZone() called

--

Even if this could be changed, and the frameworks completely audited to replace every call of copyWithZone() with a call to copy(), the fact that copyWithZone() has always been part of the API contract means that third-party libraries and frameworks could be calling it, which subsequently means that they would break if copyWithZone() were to go away. Calling copyWithZone() directly isn’t completely unheard of, actually; if an object is implementing its own copyWithZone() method and one of its properties conforms to NSCopying, it may well call copyWithZone() to do the copy, because it’s already got this zone pointer right here, and hey, why not. Add the fact that this was actually correct practice way back in the day when zones were still in use, and I’m sure there’s at least some amount of code like this out there.

One thing you could do, of course, is use compiler magic to turn override func copy() into func copyWithZone(_), but I’m not sure it’s worth it just for this.

Charles

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160206/7c7d7bba/attachment.html>


More information about the swift-evolution mailing list