D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6489 - Should be able to copy double[] to float[]
Summary: Should be able to copy double[] to float[]
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-08-13 06:41 UTC by David Simcha
Modified: 2015-06-09 05:15 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description David Simcha 2011-08-13 06:41:27 UTC
void main() {
    float[] f;
    double[] d;
    f[] = d[];
}

test9.d(4): Error: cannot implicitly convert expression (d[]) of type double[] to const(float[])
Comment 1 bearophile_hugs 2011-08-13 07:05:04 UTC
Why? And only double->float? int->long too?
Comment 2 David Simcha 2011-08-13 11:53:29 UTC
(In reply to comment #1)
> Why? And only double->float? int->long too?

Of course int -> long should work, too.  Same with anything where implicit conversions are allowed.  It's just that double -> float is the first case I stumbled upon.
Comment 3 Walter Bright 2011-08-15 11:50:21 UTC
What is the compelling rationale for this? Is there a use case that shows it?

(Also marked as an enhancement request, as it is not a bug.)
Comment 4 David Simcha 2011-08-27 07:34:39 UTC
The compelling rationale is consistency.  I can assign the elements of a float[] to a double[] one-by-one, so why not with array-wise operations?
Comment 5 bearophile_hugs 2011-08-27 16:18:55 UTC
Please show one or more use case.

Do you want it to be like a reinterpret cast? Currently this works:

void main() {
    ushort[4] a = [1, 2, 3, 4];
    uint[2] b = cast(uint[2])a;
    assert(b == [131073, 262147]);
}

Mixing reinterpret cast with conversion casts is a bad design.
Comment 6 David Simcha 2011-08-27 17:44:10 UTC
No.  The idea is that it should work exactly the same as if I'd written out the loop manually:

arrayOfFloats[] = arrayOfDoubles[]

// Equivalent to:

foreach(i, elem; arrayOfDoubles) {
    arrayOfFloats[i] = elem;
}
Comment 7 Don 2011-08-27 22:36:07 UTC
(In reply to comment #4)
> The compelling rationale is consistency.  I can assign the elements of a
> float[] to a double[] one-by-one, so why not with array-wise operations?

Actually, allowing this would introduce a huge inconsistency. The existing rule is simple and consistent: you can't mix sizes.
Suppose this were allowed. Then what about:
float [] a;
double [] b;
b[]= a[]*3.7;
So for the sake of consistency you are forced to support a huge number of operations which are very complicated to implement, and which perform so poorly, that they generally indicate a bug in the users code.

Consistency is a very, very strong argument against implementing this. There possibly could be arguments for supporting it as a special case; but they would need to be very strong, since would involve introducing an inconsistency.
Comment 8 Denis Shelomovskii 2013-11-07 11:22:04 UTC
I'll WONTFIX this terrible proposal in a try to stop it magically affect dmd to such a degree as Issue 11470.