[rust-dev] Why are generic integers not usable as floats?

SiegeLord slabode at aim.com
Fri Jun 20 05:13:03 PDT 2014


On 06/20/2014 07:36 AM, Nathan Typanski wrote:
> On 06/19, Benjamin Striegel wrote:
>> I'm actually very pleased that floating point literals are entirely
>> separate from integer literals, but I can't quite explain why. A matter of
>> taste, I suppose. Perhaps it stems from symmetry with the fact that I
>> wouldn't want `let x: int = 1.0;` to be valid.
>
> I agree that `let x: int = 1.0` should not be valid. But that is type
> *demotion*, and with `let x: f32 = 1` we are doing type *promotion*.

This isn't promotion because 1.0 does not have a concrete type. E.g. 
consider this code:

let a: f32 = 1.0;
let mut b: f64 = 1.0;
b = a as f64; // Cast has to be here

Even though we used 1.0 to initialize both f32 and f64 variables, we 
can't assign f32 to f64 without a cast.

I'm in favor of this unification and I think somebody should write an 
RFC for this.

-SL


More information about the Rust-dev mailing list