Skip to content

[Docs] Clarifies ObjectIdentifier guarantees #34349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 26, 2020

Conversation

sunbohong
Copy link
Contributor

SR-13564
rdar://69169351

/// `ObjectIdentifier` is only guaranteed to remain unique for the
/// lifetime of an object. When the instance gets deallocated, its object
/// identifier may be reused for a different object. (Internally, objects are
/// identified by their memory location.)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this parenthetical mean, and why is it important for users to know this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned on the https://bugs.swift.org/browse/SR-13564, the result of this example is not as expected.

   func makeIdentifier(input: Int) -> ObjectIdentifier {
        ObjectIdentifier(NSNumber(value:input));
    }
    
    let a = makeIdentifier(input: Int.min);
    let b = makeIdentifier(input: Int.max);
    print(a == b) // print: true

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, that result would be unexpected. However, I just ran your example, and on my machine it prints false, which is what I would expect.

Under what conditions are you getting true? I'm not sure I would understand how that comes about, and the parenthetical doesn't clarify for me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That example could give different results, depending on whether:

  • tagged pointers are supported (Apple platforms only), or
  • the allocator decides to reuse the same memory location.

(Although tagged pointers might not be used for Int.min and Int.max).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, that result would be unexpected. However, I just ran your example, and on my machine it prints false, which is what I would expect.

Under what conditions are you getting true? I'm not sure I would understand how that comes about, and the parenthetical doesn't clarify for me.

Mac x86_64 & Xcode

Comment on lines 18 to 21
/// `ObjectIdentifier` is only guaranteed to remain unique for the
/// lifetime of an object. When the instance gets deallocated, its object
/// identifier may be reused for a different object. (Internally, objects are
/// identified by their memory location.)
Copy link
Contributor

@varungandhi-apple varungandhi-apple Oct 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having wording for allocation/deallocation/memory locations here makes me a bit uncomfortable. That makes it seem like we are making some commitments about the memory allocation under the hood, but I'm not sure if we want to do that. How about something simpler:

The unique identity for a class instance is only valid for comparisons during
the lifetime of the instance. The unique identity for a metatype is always valid.

You could also omit the second sentence I suggested above if you think it's not helpful.

Comment on lines 18 to 19
/// `ObjectIdentifier` is only guaranteed to remain unique for the
/// lifetime of an object.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Sorry if this seems like nit-picking but:] This doesn't sound quite right. ObjectIdentifier is a type. It is not the type which is unique, values of that type are unique at runtime. This is why I suggested:

The unique identity for a class instance is only valid for comparisons during
the lifetime of the instance. 

The "unique identity" corresponds to values of type ObjectIdentifier, and IMO fits better stylistically with the rest of the doc comment, which also uses "unique identities" as shorthand for "values of type ObjectIdentifier".

@natecook1000
Copy link
Member

Thanks for identifying this issue and getting the docs updated, @sunbohong!

@natecook1000
Copy link
Member

@swift-ci Please smoke test

@kylemacomber kylemacomber merged commit 3a1b9cd into swiftlang:main Oct 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants