Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@omochi
Created August 8, 2019 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omochi/36e1182d51214d98681710125d436163 to your computer and use it in GitHub Desktop.
Save omochi/36e1182d51214d98681710125d436163 to your computer and use it in GitHub Desktop.
/*
ビルド & 実行
# 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