Skip to content

Instantly share code, notes, and snippets.

@soutaro
Created June 28, 2015 09:34
Show Gist options
  • Save soutaro/4c4facc1ca7dd1528deb to your computer and use it in GitHub Desktop.
Save soutaro/4c4facc1ca7dd1528deb to your computer and use it in GitHub Desktop.
Covariant Error
import Foundation
class A0 { }
class A1: A0 { }
class A2: A1 { }
class Array<X> {
func get() -> X { fatalError() }
func set(x: X) -> () { fatalError() }
}
let a0 = A0()
let a1 = A1()
let a2 = A2()
let c = Array<A1>()
let c0: Array<A0> = c // Compile error
let c1: Array<A1> = c
let c2: Array<A2> = c // Compile error
// c0.set(a0)
// c2.get() => A0 is now treated as A2
// c0.get() => c.get() as A0 => (x: A1) as A0 o
// c1.get() => c.get() as A1 => (x: A1) as A1 o
// c2.get() => c.get() as A2 => (x: A1) as A2 ???
// c0.set(a0) => A0 is treated as A1 in c0.set
// c1.set(a1) => ok
// c2.set(a2) => ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment