Vector operations doesn't convert to a common type?

bearophile bearophileHUGS at lycos.com
Sat Apr 16 06:58:58 PDT 2011


simendsjo:

>      int[3]   a = [1,2,4];
>      float[3] b = [1,2,4];
>      float[3] c;
>      // Why doesn't this work?
>      c = a[] + b[]; // Error: incompatible types for ((a[]) + (b[])): 
> 'int[]' and 'float[]'

>      // When this works?
>      c[0] = a[0] + b[0];
>      c[1] = a[1] + b[1];
>      c[2] = a[2] + b[2];
>      assert(c == [2,4,8]);

Vector ops are often implemented in assembly, and despite they are less flexible, they sometimes lead to more efficiency (if the arrays are large). The second example uses normal D code, that's much more flexible.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list