Cannot implicitly convert expression [[0, -1, 2], [4, 11, 2]] of type int[][] to const(int[2])[]

Adnan relay.public.adnan at outlook.com
Fri Jan 31 12:37:43 UTC 2020


https://wiki.dlang.org/Dense_multidimensional_arrays#Static_arrays describes a way to create static arrays:

int[3][3] matrix = [
     [ 1, 2, 3 ],
     [ 4, 5, 6 ],
     [ 7, 8, 9 ]
];

However my complains that I can't implicitly create static arrays 
from dynamic arrays.

private T[R1][C2] loopMul(ulong R1, ulong C1, ulong R2, ulong C2, 
T)(
         auto ref T[R1][C1] matrixA, auto ref T[R1][C2] matrixB) 
if (C1 == R2) {
     T[R1][C2] result;
     for (ulong r = 0; r < R1; ++r) {
         for (ulong c = 0; c < C2; ++c) {
             T toAdd = 0;
             for (ulong n = 0; n < C1; ++n) {
                 toAdd += matrixA[r][n] + matrixB[c][n];
             }
             result[r][c] = toAdd;
         }
     }
}

void main() {
     import std;

     scope (success)
         std.writeln("loopMul -- ok");

     // assert([[0, -1, 2], [4, 11, 2]].loopMul!(2, 3, 3, 2, 
int)([[3, -1], [1, 2], [6, 1]]) == [
     //         [11, 0], [35, 20]
     //         ]);
     const int[2][3] matA = [[0, -1, 2], [4, 11, 2]];
     const int[3][2] matB = [[3, -1], [1, 2], [6, 1]];
     const int[2][2] matC = [[11, 0], [35, 20]];
     assert(matA.loopMul(matB) == matC);
}


/// I would share the D online editor link but `shorten` button 
doesn't do anything
onlineapp.d(24): Error: cannot implicitly convert expression [[0, 
-1, 2], [4, 11, 2]] of type int[][] to const(int[2])[]
onlineapp.d(25): Error: cannot implicitly convert expression [[3, 
-1], [1, 2], [6, 1]] of type int[][] to const(int[3])[]
onlineapp.d(27): Error: template onlineapp.loopMul cannot deduce 
function from argument types !()(const(int[2][3]), 
const(int[3][2])), candidates are:
onlineapp.d(1):        loopMul(ulong R1, ulong C1, ulong R2, 
ulong C2, T)(auto ref T[R1][C1] matrixA, auto ref T[R1][C2] 
matrixB)

What's causing this?


More information about the Digitalmars-d-learn mailing list