Skip to content

Fixes example snippet of AnyHashable #21550

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 1 commit into from
Nov 18, 2019
Merged

Conversation

valeriyvan
Copy link
Contributor

@valeriyvan valeriyvan commented Dec 26, 2018

Otherwise, print(descriptions[AnyHashable(43)]) prints an Int8, not nil.

This PR fixes example snippet in comments making it working as described.

n/a

Otherwise, `print(descriptions[AnyHashable(45)])` prints `an Int8`, not `nil`.
@rjmccall
Copy link
Contributor

rjmccall commented Jan 2, 2019

@airspeedswift Is this expected behavior for AnyHashable?

@jckarter
Copy link
Contributor

jckarter commented Jan 2, 2019

It is.

@@ -120,7 +120,7 @@ internal struct _ConcreteHashableBox<Base : Hashable> : _AnyHashableBox {
/// AnyHashable(Set(["a", "b"])): "a set of strings"
/// ]
/// print(descriptions[AnyHashable(42)]!) // prints "an Int"
/// print(descriptions[AnyHashable(43)]) // prints "nil"
/// print(descriptions[AnyHashable(45)]) // prints "nil"
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be more illustrative to keep the descriptions[AnyHashable(43)] example here, and update the 'prints "nil"' comment to show the expected behavior.

@natecook1000
Copy link
Member

@jckarter What types are fungible like this? It seems to work for numeric types as long as there's an exact precision match (i.e. you can get 2.25 out as a Float, but not 2.2), but trying to get a string out as a character fails.

@jckarter
Copy link
Contributor

jckarter commented Jan 2, 2019

@natecook1000 AnyHashable should behave this way for any types that are transitively as?-castable, so you can get a String out as an NSString and v.v., an Array<T> as an NSArray, and so on. The standard library integer and floating-point types as well as CGFloat and Decimal all bridge to NSNumber, and NSNumber bridges back to any of those types if the value is exactly representable.

@CodaFi
Copy link
Contributor

CodaFi commented Nov 18, 2019

I think this change is good to take as-is.

@swift-ci please smoke test and merge

@swift-ci swift-ci merged commit 598042b into swiftlang:master Nov 18, 2019
@valeriyvan valeriyvan deleted the patch-1 branch February 20, 2023 08:04
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.

6 participants