Scalar + array operations

Stefan Frijters via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 21 04:45:55 PDT 2014


When working on my current project (writing a numerical 
simulation code) I ran into the following issue when trying to 
multiply a vector (represented by a fixed-length array) by a 
scalar:

import std.stdio;

void main() {
   int ifoo = 2;
   int[3] ibar = 1;

   double dfoo = 2.0;
   double[3] dbar = 1.0;

   dfoo = ifoo * dfoo;      // Scalar int * scalar double -- OK
   writeln(dfoo);
   dfoo = dfoo * dfoo;      // Scalar double * scalar double -- OK
   writeln(dfoo);
   dbar = dfoo * dbar[];    // Scalar double * array of double -- 
OK
   writeln(dbar);
   ibar = ifoo * ibar[];    // Scalar int * array of int -- OK
   writeln(ibar);
   dbar = ifoo * dbar[];    // Scalar int * array of double -- OK
   writeln(dbar);
   // dbar = dfoo * ibar[]; // Scalar double * array of int -- FAIL
   // writeln(dbar);
}

I would have expected the last case to work as well, but I get

testarr.d(20): Error: incompatible types for ((dfoo) * (ibar[])): 
'double' and 'int[]'

Is this by design? It was very surprising to me, especially since 
all other combinations do seem to work.

Kind regards,

Stefan Frijters


More information about the Digitalmars-d-learn mailing list