Reputation: 22926
Why doesn't Swift allow this generic function:
I'm trying to express a generic function that will take two "number" types, and compare them, could be Ints, Floats, Doubles etc...
Why does Swift say Same-type requirement makes generic parameters 'T' and 'U' equivalent
, what's wrong with that?
func isSameNumber <T, U> (lhs: T, rhs: U) where T: Numeric, U:Numeric, T == U {
if lhs == rhs {
print("true")
}
else{
print("false")
}
}
Upvotes: 1
Views: 378
Reputation: 54706
If T == U
, then you only have a single generic type, so don't declare 2 generic types. You simply need to constraint both input arguments to the same generic type, T
.
func isSameNumber<T>(lhs: T, rhs: T) where T: Numeric {
if lhs == rhs {
print("true")
}
else{
print("false")
}
}
Upvotes: 5