Skip to content

Commit

Permalink
[AnyHashable] Eliminate the _AnyHashableProtocol hack.
Browse files Browse the repository at this point in the history
Now that we have the ability to write extensions where one of the type
parameters is equivalent to a concrete type, eliminate
_AnyHashableProtocol and provide AnyHashable-specific behavior for
Dictionary (where Key == AnyHashable) and Set (where Element ==
AnyHashable) rather than employing the "Key: _AnyHashableProtocol"
hack.

Fixes standard library ABI FIXME's swiftlang#35, swiftlang#37, swiftlang#39.
  • Loading branch information
DougGregor authored and slavapestov committed Oct 13, 2016
1 parent 092a931 commit 25e00d7
Showing 1 changed file with 8 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@
//
//===----------------------------------------------------------------------===//

// FIXME(ABI)#35 (Concrete Same Type Requirements): This protocol exists to identify
// `AnyHashable` in conditional extensions. Replace this protocol
// with conditional extensions on `Set` and `Dictionary` "where Key ==
// AnyHashable".
public protocol _AnyHashableProtocol {
var base: Any { get }
}

extension AnyHashable : _AnyHashableProtocol {}

//===----------------------------------------------------------------------===//
// Convenience APIs for Set<AnyHashable>
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -49,8 +39,7 @@ extension Set {
}
}

// FIXME(ABI)#37 (Concrete Same Type Requirements): replace with `where Element == AnyHashable`.
extension Set where Element : _AnyHashableProtocol {
extension Set where Element == AnyHashable {
public mutating func insert<ConcreteElement : Hashable>(
_ newMember: ConcreteElement
) -> (inserted: Bool, memberAfterInsert: ConcreteElement) {
Expand All @@ -65,15 +54,15 @@ extension Set where Element : _AnyHashableProtocol {
public mutating func update<ConcreteElement : Hashable>(
with newMember: ConcreteElement
) -> ConcreteElement? {
return _concreteElement_update(with: AnyHashable(newMember) as! Element)
return _concreteElement_update(with: AnyHashable(newMember))
.map { $0.base as! ConcreteElement }
}

@discardableResult
public mutating func remove<ConcreteElement : Hashable>(
_ member: ConcreteElement
) -> ConcreteElement? {
return _concreteElement_remove(AnyHashable(member) as! Element)
return _concreteElement_remove(AnyHashable(member))
.map { $0.base as! ConcreteElement }
}
}
Expand Down Expand Up @@ -109,31 +98,30 @@ extension Dictionary {
}
}

// FIXME(ABI)#39 (Concrete Same Type Requirements): replace with `where Element == AnyHashable`.
extension Dictionary where Key : _AnyHashableProtocol {
extension Dictionary where Key == AnyHashable {
public subscript(_ key: _Hashable) -> Value? {
// FIXME(ABI)#40 (Generic subscripts): replace this API with a
// generic subscript.
get {
return self[_concreteKey: key._toAnyHashable() as! Key]
return self[_concreteKey: key._toAnyHashable()]
}
set {
self[_concreteKey: key._toAnyHashable() as! Key] = newValue
self[_concreteKey: key._toAnyHashable()] = newValue
}
}

@discardableResult
public mutating func updateValue<ConcreteKey : Hashable>(
_ value: Value, forKey key: ConcreteKey
) -> Value? {
return _concreteKey_updateValue(value, forKey: AnyHashable(key) as! Key)
return _concreteKey_updateValue(value, forKey: AnyHashable(key))
}

@discardableResult
public mutating func removeValue<ConcreteKey : Hashable>(
forKey key: ConcreteKey
) -> Value? {
return _concreteKey_removeValue(forKey: AnyHashable(key) as! Key)
return _concreteKey_removeValue(forKey: AnyHashable(key))
}
}

0 comments on commit 25e00d7

Please sign in to comment.