How does array assignment for different sized types work?
Timon Gehr
timon.gehr at gmx.ch
Thu Jan 31 01:47:05 PST 2013
On 01/31/2013 05:48 AM, estew wrote:
> void main() {
> float[3] v1 = [1.0, 2.0, 3.0]; // No error
> float[3] v = [1.0, 2.0, 3.0].dup; // Fails at runtime with error message
> }
> ...
It fails at compile time?
The reason is that array literals have special conversion rules:
Eg:
bool[] x = [0,1,0,1,0,1,1];
An array literal is converted element-wise. This means an array literal
sometimes behaves differently from other expressions of the same type:
import std.stdio;
void main() {
int[] a = [0,2,0,1];
bool[] x = cast(bool[])[0,2,0,1];
bool[] y = cast(bool[])a;
writeln(x,"\n",y);
}
[false, true, false, true]
[false, false, false, false, true, false, false, false, false, false,
false, false, true, false, false, false]
More information about the Digitalmars-d-learn
mailing list