I am trying to extend the Swift class Arraywith the following function:
func containsObjectIdenticalTo(obj: T) -> Bool {
return objectPassingTest { x in x == obj }
}
Apparently, this will not compile, since the compiler does not yet know if it is ==implemented for the type T. Then I change the code to
func containsObjectIdenticalTo(obj: T) -> Bool {
return objectPassingTest {
x in
assert(x is Equatable && obj is Equatable)
return (x as Equatable) == (obj as Equatable)
} != nil
}
This also does not work, since Equatableit is impossible to verify compliance with (since Equatableit was not determined using @obj) !
Any thoughts on this? It would be nice if there was some way to directly state if it Tmatches Equatable, but I have not read it anywhere. Swift seems less dynamic than Obj-C in these materials.
UPDATE:
I tried this sentence, and it does not work (I don’t know exactly why <T: Equatable>, it compiles).
func containsObjectIdenticalTo<T: Equatable>(obj: T) -> Bool {
var x = self[0]
var y = self[0]
return x == y
}