Why can't I pass a const array to a function that takes scope const arrays?
Paul Backus
snarwin at gmail.com
Mon Feb 17 20:53:35 UTC 2020
On Monday, 17 February 2020 at 20:08:24 UTC, Adnan wrote:
> On Monday, 17 February 2020 at 14:34:44 UTC, Simen Kjærås wrote:
>> On Monday, 17 February 2020 at 14:04:34 UTC, Adnan wrote:
>>> //
>>
>> All in all, I end up with this code:
>>
>> module strassens_matmul
>> package {
>> T[][] mulIterative(T)(const T[][] mat1, 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;
>> }
>> }
>
> Aren’t these passing by values? That’s why I used ref. Plus
> since I don’t want to change the arrays in the function scope I
> used const scope.
In D, the type `T[]` is not an array like in C, but a slice,
containing a pointer to some data and an integer indicating the
array's length. So when you pass it to a function by value, only
the pointer and length are copied, not the data itself.
You can read more about D's slices in this article:
https://dlang.org/articles/d-array-article.html
More information about the Digitalmars-d-learn
mailing list