[swift-evolution] [Discussion] Additional generics specialization

L. Mihalkovic laurent.mihalkovic at gmail.com
Mon Jul 18 14:48:12 CDT 2016



Regards
(From mobile)

> On Jul 18, 2016, at 9:04 AM, Adrian Zubarev via swift-evolution <swift-evolution at swift.org> wrote:
> 
> This is something additional, but I’m curios about how the community feels about it.
> 
> I recently come across the issue where conforming to Hashable wasn’t enough to thecke if two instances of the same generic type were equal.
> 
> I had additionally provide myself the != function.
> 
> public func !=<T, U>(lhs: SomeTypeName<T>, rhs: SomeTypeName<U>) -> Bool {
>     return lhs.hashValue != rhs.hashValue
> }
> I wondered if Swift can ever get generic specialization like this:
> 
> public func !=<T : Hashable, U, V>(lhs: T<U>, rhs: T<V>) -> Bool {
>     return !(lhs.hashValue == rhs.hashValue)
> }
I am not sure about the ordering... Interestingly enough, this is real code written last week as part of a framework:

Export function newSataStore<T extends DataStore<U>, U> (ctor: { new (config:DataStoreConfig):T, config:DataStoreConfig):T {
   return new ctor(config)
}

And this is legal code just to try:

 interface Hashable {
  getHash(): number
}
interface T extends Hashable { }
class SomeTypeName<Type extends Hashable> {
  public getHash():number {
    return ...;
  }
  public type():this {
    return Type;
  }
}
function compare<T1 extends T, T2 extends T>(lhs:SomeTypeName<T1>, rhs:SomeTypeName<T2>):boolean {
  return lhs.getHash() != rhs.getHash();
}
 
but this is more interesting:

type List<T> = T & { next: List<T> }   // listObj.next.next.next.???




> This function in stdlib would fill the gap. Or we need an extra protocol GenericHashable which includes !=.
> 
> 
> 
> -- 
> Adrian Zubarev
> Sent with Airmail
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160718/5ff46a84/attachment-0001.html>


More information about the swift-evolution mailing list