Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
ビルド & 実行
# a.swiftmoduleの生成
$ swiftc -emit-module a.swift
# liba.dylibの生成
$ swiftc -emit-library a.swift
# b.swiftの実行
$ swift -I . -L. -la b.swift
*/
// --------------------------------
// a.swift
// --------------------------------
public protocol P {}
public struct S : P {}
open class A<T> {
public init() {}
}
extension A : P where T : P {
}
open class C {
public init() {}
}
public func test1(_ x: Any) {
if x is P {
print("type of x (\(type(of: x))) conforms P")
} else {
print("type of x (\(type(of: x))) does not conforms P")
}
}
// --------------------------------
// b.swift
// --------------------------------
import a
//public class B<U> : A<Int> {}
// b.swift:5:15: error: conflicting conformance of 'B<U>' to protocol 'P'; there cannot be more than one conformance, even with different conditional bounds
// 親クラスAがすでにPのcondconfを持っているから駄目らしい
//extension B : P where U : P {}
class D<U> : C {}
extension D : P where U : P {}
let a1 = A<Int>()
test1(a1) // not conform
let a2 = A<S>()
test1(a2) // conditionally conform
let d1 = D<Int>()
test1(d1) // not conform
let d2 = D<S>()
test1(d2) // conditionally conform
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.