Is there any more elegant way than doing this to test Any for possible eqaulity?
func eq<T>(a: T, b: T) -> Bool? {
if let a = a as? Double,
let b = b as? Double {
return a == b
}
else if let a = a as? Int,
let b = b as? Int {
return a == b
}
else if let a = a as? Range<Int>,
let b = b as? Range<Int> {
return a == b
}
else if let a = a as? Bool,
let b = b as? Bool {
return a == b
}
// ..........
else {
return false
}
}
let a = reflect(2.0).value
assert(eq(a,a) == true, "should work")
Casting to Equatable doesn't work,
polymorphic functions like following don't work either,
because the dispatch is done at compile time.
func eq<T: Equatable>(a: T, _ b: T) -> Bool? {
return a == b
}
func eq<T>(a: T, _ b: T) -> Bool? {
return nil
}
let a = reflect(2.0).value
assert(eq(a,a) == true, "does not work")
What's the reason behind this?
What makes casting to a protocol with generic type requirements at runtime difficult?
Is there a workaround with dynamicType or sth. like this?