Let's say for academic purposes, I would prefer an expression like
someInt.asDouble
rather than the stock
Double(someInt)
Since all of the various Swift integer types conform to the IntegerType
protocol AND because there seems to be a Double()
initializer that fits every kind of those int types, I thought I could something like:
extension IntegerType {
var asDouble:Double {
return Double(self)
}
}
This does not work. And I would like to know why? I would like to know if there is some magic that would make it work? A where
maybe? Or something to do with the self
reference?
The error I get in the console of the playground I tried in this reads:
Playground execution failed: /var/folders/2k/6y8rslzn1m95gjpg534j7v8jzr03tz/T/./lldb/41416/playground37.swift:31:10: error: cannot invoke initializer for type 'Double' with an argument list of type '(Self)'
return Double(self)
^
/var/folders/2k/6y8rslzn1m95gjpg534j7v8jzr03tz/T/./lldb/41416/playground37.swift:31:10: note: overloads for 'Double' exist with these partially matching parameter lists: (Double), (UInt8), (Int8), (UInt16), (Int16), (UInt32), (Int32), (UInt64), (Int64), (UInt), (Int), (Float), (Float80), (String), (CGFloat), (NSNumber)
return Double(self)