Skip to content

Instantly share code, notes, and snippets.

@tarunon
Created April 14, 2017 00:37
Show Gist options
  • Save tarunon/bea3c5cd8d0c8f8c2a2ffb5b9c86e748 to your computer and use it in GitHub Desktop.
Save tarunon/bea3c5cd8d0c8f8c2a2ffb5b9c86e748 to your computer and use it in GitHub Desktop.
protocol A {}
struct B: A{}
let tuple1: (A, Int) = (B(), 1)
let tuple2: (B, Int) = (B(), 1)
// let tuple3: (A, Int) = tuple2 // 共変っぽいけどだめ。
let closure1: (A, Int) -> () = { _ in }
let closure2: (B, Int) -> () = { _ in }
let closure3: (B, Int) -> () = closure1 // 反変。いける。
let tupleClosure1: ((A, Int), Int) -> () = { _ in }
let tupleClosure2: ((B, Int), Int) -> () = { _ in }
let tupleClosure3: ((B, Int), Int) -> () = tupleClosure1 // Tupleの共変サポートされてないのにここはいけるのか
class MyG<T> {
var genericsClosure: (T, Int) -> () = { _ in }
}
let g = MyG<(B, Int)>()
g.genericsClosure = tupleClosure1 // Generics型でも動いてる
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment