-
Notifications
You must be signed in to change notification settings - Fork 14
How are you auto-casting with just the = sign #17
Comments
@eonist Sorry, it looks like the The only thing possible today, is what's defined in the test-suite(s). Sorry for the inconvenience. |
So something like this: let someDouble:Double = 4
let someFloat:CGFloat = someDouble Won't work in swift 3.0? |
Not without some Arithmetic operation, e.g addition. Since we don’t got implicit conversion on assignment, the next best thing is implicit conversion through operators.
|
Right. Would be so awesome to not worry about casting Numerical types. I guess one could make an universal cast method by using inference and generics. like: let someFloat:Float = 4
let someInt:Int = someFloat.cast()
let someDouble:Double = someFloat.cast()
let someCGFloat:CGFloat = someFloat.cast()
extension Float{
func <T>cast()->T{
if(T is Int){return Int(self)}
else if(T is Double){return Double(self)}
else if(T is CGFloat){return CGFloat(self)}
else {fatalError("numerical type not supported")}
}
} Better yet. Make a protocol with an extension that every numerical type could extend. So that you only would have to write the extension once. extension Float:NumericalConversional... and extension CGFloat:NumericalConversional...etc |
That’s actually been on my mind. But not sure about the implications.
|
What i personally do Is: let someCGFloat:CGFloat = 4
let someInt:Int = someCGFloat.int
let someDouble:Int = someCGFloat.double
let someString:String = someCGFloat.string But this solution has code all over the place. But the suggested cast solution would be more centralised and easier to reason with. Thats why I stared your project , I tought you had found a way to infer types with the "=" sign. :) |
I am sorry, I did in Swift 1.0. But they sorta removed it, and I understand since implicit casts could be dangerous.
|
Yes, definitely avoid the huge if else clause with polymorphism. And just extend every Numerical type instead. If its all in one .swift class its easy enough to reason with. |
If you have arrays with numbers you can also do something like this: let someFloats:[Float] = [1,2,3,4]
let someCGFloats:[CGFloat] = someFloats.cast()
extension Array{
func cast<T:NumericConversional>() -> [T]{
return self.map { $0.cast() }
}
} NOTE: NumericConversional would need to be a protocol that you add when you make the extension for each Number type. etc etc |
For Sequence extensions - You'd probably want to use either a flatMap or rethrow errors. |
Isn't flatmap used when a value can be nil? Are you thinking about numerical values that can be NaN? I can't remember but I think Int or UInt can't be NaN |
Yep, not to mention between unsigned and signed and such. I recall seeing optional inits. But I might be mistaken. Sent from an iPhone on the go.
|
unsigned and signed == special cases no? Anyways I usually just write for the simplest case first, if something needs more features then one could method overload... like for cases where the array items can be nil: func cast<T?:NumericConversional>() -> [T?]{
return self.flatMap { $0.cast() }
} Also i'm not sure if flatMap handles NaN values. asserting for isNaN could be a substitute. |
flatMap doesnt out of the box, but you'd have assertions in there on whatever protocol you have. |
IN your sample code you have:
But I can't find the auto infer code in your src. Some sort of extension or?
The text was updated successfully, but these errors were encountered: