Why can't I pass a const array to a function that takes scope const arrays?

Adnan relay.public.adnan at outlook.com
Mon Feb 17 14:04:34 UTC 2020


On Monday, 17 February 2020 at 13:44:55 UTC, Adnan wrote:
> https://ideone.com/lVi5Uy
>
> module strassens_matmul;
>
> debug {
>     static import std;
> }
> ...

Okay I changed to
module strassens_matmul;

debug {
     static import std;
}

package {
     ulong getRowSize(T)(scope const T[][] mat) {
         return mat[0].length;
     }

     ulong getColumnSize(T)(scope const T[][] mat) {
         return mat.length;
     }

     T[][] createMatrix(T)(scope const ulong rowSize, scope const 
ulong columnSize) {
         return new T[][](rowSize, columnSize);
     }

     /// row and column are 0 index-based
     T* getPointPtr(T)(scope const ref T[][] mat, scope const 
ulong row, scope const ulong column) {
         return &mat[row][column];
     }

     T getPointCopy(T)(scope const ref T[][] mat, scope const 
ulong row, scope const ulong column) {
         return mat[row][column];
     }

     T[][] mulIterative(T)(scope const ref  T[][] mat1, scope 
const T[][] mat2) {
         auto result = createMatrix!T(getRowSize!T(mat1), 
getColumnSize!T(mat2));
         foreach (row; 0 .. mat1.getRowSize()) {
             foreach (column; 0 .. mat2.getColumnSize()) {
                 T value;
                 foreach (i; 0 .. mat1.getRowSize()) {
                     value += mat1.getPointCopy(row, i) * 
mat2.getPointCopy(i, column);
                 }
                 *result.getPointPtr(row, column) = value;
             }
         }
         return result;
     }
}

unittest {
     const uint[][] matA = [[10, 20, 10], [4, 5, 6], [2, 3, 5]];
     const uint[][] matB = [[3, 2, 4], [3, 3, 9], [4, 4, 2]];
     const uint[][] matC = [[130, 120, 240], [51, 47, 73], [35, 
33, 45]];
     assert(matA.mulIterative(matB) == matC);
}

///

cdsa ~master: building configuration "cdsa-test-library"...
source/strassens_matmul.d(22,16): Error: cannot implicitly 
convert expression &mat[row][column] of type const(uint)* to uint*
source/strassens_matmul.d(37,36): Error: template instance 
strassens_matmul.getPointPtr!uint error instantiating
source/strassens_matmul.d(48,29):




More information about the Digitalmars-d-learn mailing list