swift3 - Are Doubles Comparable in Swift -
swift 3, xcode 8β2.
i've implemented class sorts comparable data , attempted test using doubles. unfortunately, i'm getting error "cannot convert value of type '[double]' specified type '[t]'."
. i've reproduced error in sample code below:
class foo<t: comparable> { let data: [t] = [double]() }
am right assuming doubles comparable and, if so, how eliminate error above?
thx.
your property has generic type, t
: can't declared holding concrete type. use [t]
in declaration, , use double
when instantiating class.
for example i've made property var modify it:
class foo<t: comparable> { var data: [t] = [t]() } let f = foo<double>() f.data = [42.0, 33.0] f.data.sort() print(f.data)
gives
[33.0, 42.0]
as expected.
Comments
Post a Comment