Bug 6489 – Should be able to copy double[] to float[]
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-08-13T06:41:00Z
Last change time
2015-06-09T05:15:05Z
Assigned to
nobody
Creator
dsimcha
Comments
Comment #0 by dsimcha — 2011-08-13T06:41:27Z
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 by bearophile_hugs — 2011-08-13T07:05:04Z
Why? And only double->float? int->long too?
Comment #2 by dsimcha — 2011-08-13T11:53:29Z
(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 by bugzilla — 2011-08-15T11:50:21Z
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 by dsimcha — 2011-08-27T07:34:39Z
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 by bearophile_hugs — 2011-08-27T16:18:55Z
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 by dsimcha — 2011-08-27T17:44:10Z
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 by clugdbug — 2011-08-27T22:36:07Z
(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 by verylonglogin.reg — 2013-11-07T11:22:04Z
I'll WONTFIX this terrible proposal in a try to stop it magically affect dmd to such a degree as Issue 11470.