dmd failed or dub wrong?

Mike Parker via Digitalmars-d digitalmars-d at puremagic.com
Tue May 24 20:07:31 PDT 2016


On Tuesday, 24 May 2016 at 23:47:25 UTC, mogu wrote:
> I use dub 0.9.25, DMD64 D Compiler v2.071.0. Create a minimal 
> project and write the following codes in main function.
>
> ```
> import std.math;
> long i = 0;
> i += 3.5.floor;
> ```
>
> Then run dub will get a warning and dmd failed.
>
> ```
> source/app.d(4,7): Warning: long += double is performing 
> truncating conversion
> dmd failed with exit code 1.
> ```
>

This is because dub is apparently compiling with warnings 
enabled. If you compile manually with -w, you will get the same 
result.

> But build the app.d directly use dmd. It's OK. However if the 
> last line of code above is `i = 3.5.floor;`. An error occurs.
>
> ```
> Error: cannot implicitly convert expression (floor(3.5)) of 
> type double to long
> ```
>
> So what's the deal?

Take a look at [1] and you'll see that floating point types are 
not implicitly convertible to integrals, so the error is 
appropriate.

As for the += situation, because one operand is a double, the 
other will be converted to a double for the operation. Because of 
the implicit conversion rules, it may seem that an error should 
occur because of the assignment part in the +=. This isn't 
something I've really considered before, but I assume the rule is 
ignored here because += would not be very useful if you had to 
cast the result back to the original type. Where would you place 
the cast? That's why you get the truncation warning when 
compiling with -w instead of an error about implicit conversions.

https://dlang.org/spec/type.html#usual-arithmetic-conversions


More information about the Digitalmars-d mailing list