[swift-evolution] @NSCopying currently does not affect initializers
Torin Kwok
torin at kwok.im
Sat Jan 28 01:30:12 CST 2017
To better demonstrate the problems I’m describing, I gave out a simple demo running in Playground to reproduce that. Apart from that, I also attached a Playground file that contains identical content in this mail, for the convenience of examination.
import Foundation
class Person: NSObject,
NSCopying {
var firstName: String
var lastName: String
var job: String?
init( firstName: String, lastName: String, job: String? = nil ) {
self.firstName = firstName
self.lastName = lastName
self.job = job
super.init()
}
/// Conformance to <NSCopying> protocol
func copy( with zone: NSZone? = nil ) -> Any {
let theCopy = Person.init( firstName: firstName, lastName: lastName )
theCopy.job = job
return theCopy
}
override var description: String {
return "\(firstName) \(lastName)" + ( job != nil ? ", \(job!)" : "" )
}
}
let johnAppleseed = Person( firstName: "John", lastName: "Appleseed", job: "CEO" )
var refJohnAppleseed = johnAppleseed
// assigning wihtout copying semantic:
refJohnAppleseed.job = "Engineer"
// `cloneJohnAppleseed` and `johnAppleseed` have the identical `job` ...
refJohnAppleseed
johnAppleseed
// ... and the assertion **would not** fail:
assert( refJohnAppleseed === johnAppleseed )
// Assigning a copy of johnAppleseed to clonedJohnAppleseed,
// which was returned by `copy( zone: ) -> Any`
var clonedJohnAppleseed = johnAppleseed/* refJohnAppleseed is also okay */.copy() as! Person
clonedJohnAppleseed.job = "Designer"
johnAppleseed
// Alright you see, setting the job of `clonedJohnAppleseed` doesn't affect the
// job stored in `johnAppleseed`.
//: Up to now, everything goes right. However, when we begin introducing a new class consuming instances of `Person` class...
class Department: NSObject {
// Here, we're expecting that `self.employee` would automatically
// store the deeply-copied instance of `Person` class
@NSCopying var employee: Person
init( employee _ExternalPerson: Person ) {
// CAUTION! That's the key point:
// `self.employee` has been marked with `@NSCopying` attribute
// but what would take place here is only the shallow-copying.
// In the other words, `self.employee` will share identical underlying
// object with `_ExternalPerson`.
self.employee = _ExternalPerson
super.init()
// Assertion will definitely fail since Swift do not actually
// copy the value assigned to this property even though
// `self.employee` has been marked as `@NSCoyping`:
/* assert( self.employee !== employee ) */
}
override var description: String {
return "A Department: [ ( \(employee) ) ]"
}
}
let isaacNewton = Person( firstName: "Isaac", lastName: "Newton", job: "Mathematician" )
let lab = Department.init( employee: isaacNewton )
isaacNewton.job = "Astronomer"
lab.employee
// Oops! Setting the job of `isaacNewton` affects the job stored in `lab.employee`.
// That's an unexpected behavior as we have declared `employee` property as
// `@NSCopying`. Obviously, `@NSCopying` semantic became effectless implicitly
// within the initializer of `Department` class.
// For the moment, we're required to explictly invoke `copy()` method on instances
// that wanna be copied to make sure that classes' properties are able to store
// deeply-copied results during the initialization:
/* self.employee = _ExternalPerson.copy() as! Person */
//: What indeed makes me confusing is that...
// @NSCopying semantic does work properly within the scope other than initializers:
lab.employee = isaacNewton
isaacNewton.job = "Physicist"
lab.employee
// That's it! If we assigned external instance to `lab.employee`,
// `@NSCopying` semantic would be well respected.
> On 28 Jan 2017, at 12:34, Torin Kwok <torin at kwok.im> wrote:
>
> Hello guys,
>
> Note: This issue has been originally presented in swift-users mailling list <https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20170123/004552.html>. And then I post it again here at the suggestion <https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20170123/004561.html> of Jordan Rose:
>
> It might be reasonable to change this behavior, but it probably deserves a bit of discussion on swift-evolution; it's not 100%, for-sure a bug.
> --- the original content follows this line ---
>
> I encountered a strange behavior when I declared a property with the @NSCopying attribute:
>
> // `Person` class inherits from `NSObject` class and conforms to `NSCopying` protocol
> @NSCopying var employee: Person
> and then assigned an external instance of Person class protocol to this property within the designated init methods:
>
> // Designated initializer of `Department` class
> init( employee externalEmployee: Person ) {
> self.employee = externalEmployee
> super.init()
>
> // Assertion would fail since Swift do not actually copy the value assigned to this property
> // even though `self.employee` has been marked as `@NSCoyping`
> // assert( self.employee !== externalEmployee )
> }
> If I indeed require the deep copying behavior during the init process, instead of taking advantage of @NSCopying attribute, I would have to invoke the copy() method manually:
>
> init( employee externalEmployee: Person ) {
> // ...
> self.employee = externalEmployee.copy() as! Person
> // ...
> }
> In fact, what really makes me confusing is that @NSCopying semantic does work properly within the other parts of the class definition such as normal instance methods, or external scope. For instance, if we're assigning an external instance of Person to the self.employee proper of Department directly through setter rather than initializer:
>
> department.employee = johnAppleseed
> then self.employee property and johnAppleseed variable will no longer share the same underlying object now. In the other words, @NSCopying attribute makes sense.
>
> After I looked through a great deal of results given by Google, and dicussions on StackOverflow, I finally end up with nothing helpful — the vast majority of articles, documentations as well as issues talking about this similar topics only focus on the basic concepts and effects of @NSCopying itself but do not mentioned this strange behavior at all — besides one radar descriping the same problem (rdar://21383959 <rdar://21383959>) and a final conclusion mentioned in a guy's Gist comment: ... values set during initialization are not cloned ...
>
> That is, @NSCopying semantic has no effect in initializers.
>
> Then, what I want to figure out is the reason why @NSCopying semantic will become effectless implicitly whithin initializers of a class, and the special considerations behind this behavior, if any.
>
> --- END ---
>
> Jordan:
>
> Your observation is correct: @NSCopying currently does not affect initializers. This is because accessing a property in an initializer always does direct access to the storage rather than going through the setter.
> I have tested the identical logic in Objective-C and the NSCopying semantic works perfectly within Obj-C's class initializer.
>
> I have no idea whether it's a bug or special consideration. After all, as a special consideration, it seems too strange that this behavior has not been obviously documented.
>
>
>
> Best Regards,
> Torin Kwok
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170128/49addc4a/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: nscopying_semantic_demo.playground.zip
Type: application/zip
Size: 13014 bytes
Desc: not available
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170128/49addc4a/attachment.zip>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170128/49addc4a/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3561 bytes
Desc: not available
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170128/49addc4a/attachment.p7s>
More information about the swift-evolution
mailing list