Skip to content

Instantly share code, notes, and snippets.

@ukitaka
Last active December 21, 2016 08:42
Show Gist options
  • Select an option

  • Save ukitaka/dcf69c35eb82e96e44035fa424b670f5 to your computer and use it in GitHub Desktop.

Select an option

Save ukitaka/dcf69c35eb82e96e44035fa424b670f5 to your computer and use it in GitHub Desktop.
Contravariant?
typealias Contravariant<A> = (A) -> ()
class Hoge { }
class Fuga: Hoge { }
let fuga: Fuga = Fuga()
let hoge: Hoge = Hoge()
let fugaC: Contravariant<Fuga> = { _ in }
let hogeC: Contravariant<Hoge> = { _ in }
fuga is Hoge // true
hoge is Hoge // true
fugaC is Contravariant<Fuga> // true
hogeC is Contravariant<Fuga> // false ... not true
func myFunc(_ cf: Contravariant<Fuga>) {
print("OK")
}
myFunc(fugaC) // OK
myFunc(hogeC) // OK ... (°_°)
@pascaljette
Copy link

weird... I wouldn't have expected myFunc(hogeC) to work....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment