0

Is there a good reason why this fails:

class Test<T: Hashable> {}

var d: Test<AnyHashable>? = nil
let t = Test<String>()
d = t // Cannot assign value of type 'Test<String>' to type 'Test<AnyHashable>?'

while this obviously works ?

var d: AnyHashable? = nil
let t = "123"
d = t // Works fine
mugx
  • 9,869
  • 3
  • 43
  • 55
Guig
  • 9,891
  • 7
  • 64
  • 126
  • Generics are invariant, therefore `Test` is a completely unrelated type to `Test` – see [How do I store a value of type Class in a Dictionary of type \[String:Class\] in Swift?](http://stackoverflow.com/q/38590548/2976878) & [Swift generic coercion misunderstanding](http://stackoverflow.com/q/41976844/2976878) – Hamish Feb 02 '17 at 18:59
  • Thanks. That's really annoying! – Guig Feb 02 '17 at 19:14

0 Answers0