[swift-evolution] Proposal: Auto-convert for numbers when safe

Chris Lattner clattner at apple.com
Sun Dec 6 01:30:37 CST 2015


> On Dec 5, 2015, at 11:21 PM, Manav Gabhawala <manav1907 at gmail.com> wrote:
> 
> One suggestion of a way to get around the problem of promotions could be implicit initializers.

Swift has already had a feature to support arbitrary implicit conversion between types: it was madness and got ripped out :-)

I’m not opposed to allowing user-defined implicit conversions, but IMO they need to be limited to a DAG of subtype relationships.

-Chris


> So you could have one parameter initializers that can be marked with a keyword/attribute like implicit and the compiler can automatically insert the right the initializer in place (kind of like C++ does it except the keyword implicit would be an antonym to the explicit keyword in C++). This could potentially solve the type checker speed and also allow you to move this implementation to the stdlib. It could also potentially have some interesting use cases outside of the std library too. We would also have to consider whether implicit initializers could throw errors and whether they could be failable or not.
> IMO implicit initializers should not be able to throw but should be allowed to be failable. For instance, one could have an implicit initializer from a String to NSURL but have it failable too and use a guard let/if let to bind to it (see example later)
> 
> So something along the lines of:
> 
> 	
> class Double {
> 	implicit init(_ f: Float) {
> 		// initializes a double from a float.
> 	}
> }
> 
> // For the String -> NSURL
> class NSURL { 
> 	implicit init?(_ str: String) {
> 		// initializes the usual way.
> 	}
> }
> // And then in at the call site
> guard let URL : NSURL = someString
> else { return  }
> // Do something with someString.
> 
> 
> However, because of how Swift is structured and its emphasis on being explicit (which is a great thing), this should only be used sparingly and should be discouraged unless it absolutely makes sense.
> 
> Regards,
> Manav Gabhawala
> 
> On December 6, 2015 at 2:02:51 AM, Chris Lattner via swift-evolution (swift-evolution at swift.org <mailto:swift-evolution at swift.org>) wrote:
> 
>> On Dec 5, 2015, at 4:27 AM, Jonathan Hull <jhull at gbis.com <mailto:jhull at gbis.com>> wrote:
>> > I understand why you can’t auto-convert from a Double to a Float or Int32 to Int8. It is good that we have to add the cast explicitly and think though the implications.
>> > 
>> > …but I don’t think through the implications because we currently have a boy who cried wolf situation where we have to explicitly cast everything (even the safe stuff).
>> > 
>> > 
>> > I think all of the numeric types should be able to auto-convert if the conversion is safe (without loss of precision or overflow).
>> > 
>> > For example:
>> > • If an Int is casting to a larger size (Int16 -> Int32)
>> > • Float -> Double
>> > • Float -> CGFloat
>> > • Int -> Float, Double, or CGFloat (but not the other way)
>> > 
>> > I don’t see why these aren’t allowed. The forced casts make my code much less readable. Are the casts above dangerous in a way I am not aware of?
>> 
>> I agree that the current Swift numerics model is suboptimal, I personally would like to see small integers implicitly promote to large integers (when they are known lossless), have Float promote to Double, and have both Float and Double promote to CGFloat (yes, I know that the Double -> CGFloat promotion would be lossy on 32-bit apple platforms). I personally don’t think that integer -> floating point promotions are a good idea even if value preserving, since their domains are so different.
>> 
>> The problem with doing this today is that there are a lot of dependencies we need to get resolved first.
>> 
>> 1. The type checker is really slow, partially because of too-many and too-crazy implicit conversions. We also get very surprising behavior when they kick in. Specifically, IMO, we need to reevaluate the T! <-> T and T to T? conversions. We have thoughts on this, but should be discussed in a separate thread if you’re interested.
>> 
>> 2. These promotions should be expressible in the library, not hard coded into the compiler. This means that we would need a language feature to (e.g.) be able to define subtype relationships between structs. Such a feature would be generally useful and could allow us to push some of our existing compiler magic out to the stdlib.
>> 
>> 3. We want the existing work to revise the numerics protocols to be better understood and hopefully implemented.
>> 
>> There are also a ton of unrelated specific problems that should be addressed in various ways: e.g. macros like M_PI get imported as Double instead of a typeless literal, forcing tons of casts in code that wants to use it (e.g.) with Floats. These issues are separable, and blocked on things like generic properties not being in place.
>> 
>> It would be great for interested contributors to start pushing on any of the above issues to help unblock progress on improving the numerics model.
>> 
>> -Chris
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151205/f96f8409/attachment.html>


More information about the swift-evolution mailing list