Created
April 23, 2020 04:59
-
-
Save dabrahams/612d44de33bb10c9d1549fbef0e0d36d to your computer and use it in GitHub Desktop.
Test for conformance to non-existential protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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