[Review] SE-0131: Add AnyHashable to the standard library


(Chris Lattner) #1

Hello Swift community,

The review of "SE-0131: Add AnyHashable to the standard library" begins now and runs through July 25. This proposal is a bit unusual, since it is a late additive proposal. The reason we need to do something here is as a critical part of "SE-0116: Import Objective-C id as Swift Any type”, enabling importing untyped NSDictionary's and NSSet’s properly. The core team considers this already conceptually approved for Swift 3 as part of SE-0116, but would greatly appreciate feedback on the details of the proposed design.

The proposal is available here:

  https://github.com/apple/swift-evolution/blob/master/proposals/0131-anyhashable.md

Reviews are an important part of the Swift evolution process. All reviews should be sent to the swift-evolution mailing list at

  https://lists.swift.org/mailman/listinfo/swift-evolution

or, if you would like to keep your feedback private, directly to the review manager.

What goes into a review?

The goal of the review process is to improve the proposal under review through constructive criticism and contribute to the direction of Swift. When writing your review, here are some questions you might want to answer in your review:

  * What is your evaluation of the proposal?
  * Is the problem being addressed significant enough to warrant a change to Swift?
  * Does this proposal fit well with the feel and direction of Swift?
  * If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
  * How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

More information about the Swift evolution process is available at

  https://github.com/apple/swift-evolution/blob/master/process.md

Thank you,

-Chris Lattner
Review Manager


(Nate Cook) #2

  https://github.com/apple/swift-evolution/blob/master/proposals/0131-anyhashable.md

+1!

This proposal looks great to me. I only have one nitpick: it looks like there's a '_Hashable' protocol used in the Dictionary extension to get around the limit on generic subscripts. Swift has managed to remove most (all?) underscored protocols from public APIs, so I would just suggest that the implementation consider an alternate name that isn't underscored. Something like 'HashableBase' would still imply that the protocol is more of an implementation detail than a useful protocol on its own.

Nate


(Pyry Jahkola) #3

Hello Swift community,

The review of "SE-0131: Add AnyHashable to the standard library" begins now and runs through July 25. This proposal is a bit unusual, since it is a late additive proposal. The reason we need to do something here is as a critical part of "SE-0116: Import Objective-C id as Swift Any type”, enabling importing untyped NSDictionary's and NSSet’s properly. The core team considers this already conceptually approved for Swift 3 as part of SE-0116, but would greatly appreciate feedback on the details of the proposed design.

The proposal is available here:

  https://github.com/apple/swift-evolution/blob/master/proposals/0131-anyhashable.md

  * What is your evaluation of the proposal?

+1, this is a nice addition, and useful at times.

There's one very obscure gotcha <https://swiftlang.ng.bluemix.net/#/repl/c1ddd24113169ab82df118660c8e0de6ea24e48d32997c327638a88dc686e91f> with AnyHashable that subclass instances of a Hashable base class should be cast to the base type before wrapping into AnyHashable, otherwise such wrapped values may break the equivalence law of symmetry (`a == b` iff `b == a`).

But now that most of Foundation's class clusters are bridged into value types, that problem is unlikely to occur to anyone anymore.

  * Is the problem being addressed significant enough to warrant a change to Swift?

So and so.

  * Does this proposal fit well with the feel and direction of Swift?

Yes. Maybe it will also help us gain more experience when designing real existential types.

  * If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?

A bit of a hack compared to real existentials. But a good one.

  * How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

Quick reading.

— Pyry

···

On 24 Jul 2016, at 01:26, Chris Lattner via swift-evolution <swift-evolution@swift.org> wrote:


(Brent Royal-Gordon) #4

Skipping the usual rigamarole:

Looks great, and quite aside from its use in bridged dictionaries, I'm sure users will happily abuse it for heterogeneous comparisons.

My only comment is this: Is the `base` property following some convention established by other type-erased wrappers? If not, I would suggest either `any`, `value`, or `wrapped`; `base` seems to suggest something like a base class, which is not what's going on here.

···

On Jul 23, 2016, at 3:26 PM, Chris Lattner <clattner@apple.com> wrote:

The review of "SE-0131: Add AnyHashable to the standard library" begins now and runs through July 25. This proposal is a bit unusual, since it is a late additive proposal. The reason we need to do something here is as a critical part of "SE-0116: Import Objective-C id as Swift Any type”, enabling importing untyped NSDictionary's and NSSet’s properly. The core team considers this already conceptually approved for Swift 3 as part of SE-0116, but would greatly appreciate feedback on the details of the proposed design.

--
Brent Royal-Gordon
Architechies


(Karl) #5

I’d only +1 this proposal if we can perform an implicit conversion. From SE-0116:

The user model for this type would ideally align with our long-term goal of supporting Hashable existentials directly, so the type deserves some short-term compiler support to help us get there.

I read that as meaning that the interface should look as close as possible to what using a raw “Hashable” would look like, and that some short-term complier magic to achieve that would be acceptable. As far as that goes:

- unwrapping is functionally equivalent (upcasting from Any today, in the future from Hashable, but when upcasting all that matters is the destination type so NFC)
- wrapping is too verbose:

/// let descriptions: [AnyHashable : Any] = [
/// AnyHashable(":smile:"): "emoji",
/// AnyHashable(42): "an Int",
/// AnyHashable(Int8(43)): "an Int8",
/// AnyHashable(Set(["a", "b"])): "a set of strings"
/// ]

That would be a big regression in readability, IMO. Previously, you could declare those keys without the “AnyHashable” wrapper, and the bridging magic would turn Int -> NSNumber because that’s the way to make an Int conform to NSObject. It should be possible to teach the compiler how to turn a Hashable in to an AnyHashable implicitly.

But yeah, if we can do an implicit conversion, it’s a :+1: from me

Karl

···

On 24 Jul 2016, at 00:26, Chris Lattner via swift-evolution <swift-evolution@swift.org> wrote:

Hello Swift community,

The review of "SE-0131: Add AnyHashable to the standard library" begins now and runs through July 25. This proposal is a bit unusual, since it is a late additive proposal. The reason we need to do something here is as a critical part of "SE-0116: Import Objective-C id as Swift Any type”, enabling importing untyped NSDictionary's and NSSet’s properly. The core team considers this already conceptually approved for Swift 3 as part of SE-0116, but would greatly appreciate feedback on the details of the proposed design.

The proposal is available here:

  https://github.com/apple/swift-evolution/blob/master/proposals/0131-anyhashable.md

Reviews are an important part of the Swift evolution process. All reviews should be sent to the swift-evolution mailing list at

  https://lists.swift.org/mailman/listinfo/swift-evolution

or, if you would like to keep your feedback private, directly to the review manager.

What goes into a review?

The goal of the review process is to improve the proposal under review through constructive criticism and contribute to the direction of Swift. When writing your review, here are some questions you might want to answer in your review:

  * What is your evaluation of the proposal?
  * Is the problem being addressed significant enough to warrant a change to Swift?
  * Does this proposal fit well with the feel and direction of Swift?
  * If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
  * How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

More information about the Swift evolution process is available at

  https://github.com/apple/swift-evolution/blob/master/process.md

Thank you,

-Chris Lattner
Review Manager

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Dmitri Gribenko) #6

The implementation that I have does not suffer from this issue. (The
initializer of AnyHashable walks the superclass chain and upcasts the
value to the correct type.)

Dmitri

···

On Mon, Jul 25, 2016 at 1:34 PM, Pyry Jahkola via swift-evolution <swift-evolution@swift.org> wrote:

On 24 Jul 2016, at 01:26, Chris Lattner via swift-evolution > <swift-evolution@swift.org> wrote:

Hello Swift community,

The review of "SE-0131: Add AnyHashable to the standard library" begins now
and runs through July 25. This proposal is a bit unusual, since it is a
late additive proposal. The reason we need to do something here is as a
critical part of "SE-0116: Import Objective-C id as Swift Any type”,
enabling importing untyped NSDictionary's and NSSet’s properly. The core
team considers this already conceptually approved for Swift 3 as part of
SE-0116, but would greatly appreciate feedback on the details of the
proposed design.

The proposal is available here:

https://github.com/apple/swift-evolution/blob/master/proposals/0131-anyhashable.md

* What is your evaluation of the proposal?

+1, this is a nice addition, and useful at times.

There's one very obscure gotcha with AnyHashable that subclass instances of
a Hashable base class should be cast to the base type before wrapping into
AnyHashable, otherwise such wrapped values may break the equivalence law of
symmetry (`a == b` iff `b == a`).

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/


(Dmitri Gribenko) #7

Hi Nate,

I don't see a benefit for this. Nobody should be using _Hashable.
Adding it as a non-underscored protocol increases API surface for no
benefit. _Hashable is slated for removal as soon as we get the
necessary compiler features.

Dmitri

···

On Mon, Jul 25, 2016 at 1:12 PM, Nate Cook via swift-evolution <swift-evolution@swift.org> wrote:

      https://github.com/apple/swift-evolution/blob/master/proposals/0131-anyhashable.md

+1!

This proposal looks great to me. I only have one nitpick: it looks like there's a '_Hashable' protocol used in the Dictionary extension to get around the limit on generic subscripts. Swift has managed to remove most (all?) underscored protocols from public APIs, so I would just suggest that the implementation consider an alternate name that isn't underscored. Something like 'HashableBase' would still imply that the protocol is more of an implementation detail than a useful protocol on its own.

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/


(Pyry Jahkola) #8

Dmitri Gribenko wrote:

There's one very obscure gotcha with AnyHashable that subclass instances of
a Hashable base class should be cast to the base type before wrapping into
AnyHashable, otherwise such wrapped values may break the equivalence law of
symmetry (`a == b` iff `b == a`).

The implementation that I have does not suffer from this issue. (The
initializer of AnyHashable walks the superclass chain and upcasts the
value to the correct type.)

Brilliant! Glad that you found a resolution!

— Pyry