Skip to content

Instantly share code, notes, and snippets.

@dabrahams
Created April 23, 2020 04:59
Show Gist options
  • Save dabrahams/612d44de33bb10c9d1549fbef0e0d36d to your computer and use it in GitHub Desktop.
Save dabrahams/612d44de33bb10c9d1549fbef0e0d36d to your computer and use it in GitHub Desktop.
Test for conformance to non-existential protocol
protocol True {}
struct IsBidirectional<C> {}
extension IsBidirectional: True where C : BidirectionalCollection { }
extension Collection {
var isBidirectional: Bool {
return IsBidirectional<Self>() is True
}
}
func testIsBidirectional() {
func check<C: Collection>(_ collection: C, isBidirectional: Bool) {
assert(collection.isBidirectional == isBidirectional)
}
check([1, 2, 3], isBidirectional: true)
check(AnyBidirectionalCollection([1, 2, 3]), isBidirectional: true)
check(AnyCollection([1, 2, 3]), isBidirectional: false)
check(Set([1, 2, 3]), isBidirectional: false) // Dang, Set still isn't bidirectional?! :(
}
testIsBidirectional()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment