So far, the simplest solution is to create a separate function
for a two-dimensional array:
```d
auto copy2DArray(T)(const T[][] arr) if (!is(T == class)) {
T[][] copy;
foreach(row; arr) {
T[] tmp;
foreach(field; row) {
tmp ~= field;
}
copy ~=
More accurately:
```d
T[] copyArray(T)(inout(T)[] arr) {
T[] copy = new T[arr.length];
static if (is(T == U[], U) && !is(T == immutable(Y)[], Y)) {
foreach(i, ref v; copy) {
v = copyArray(arr[i]);
}
} else static if (is(T == immutable(W)[], W)) {
fo
I'm trying to write a function for copying an array. But
something strange is happening with the const correctness. I
cannot remove the constitution for the case of a two-dimensional
array of lines.
```d
import std.stdio;
import std.traits : Unconst;
auto copyArray(T)(const T[] arr) if (!is(T
On Tuesday, 7 October 2025 at 19:07:28 UTC, Vindex9 wrote:
alternative to Dub
* alternative to dup property
On Wednesday, 8 October 2025 at 02:58:15 UTC, Steven
Schveighoffer wrote:
My attempt:
```d
import std.traits;
inout(T)[] copyArray(T)(inout(T)[] arr) {
alias M = Unqual!T;
```
Unfortunately, `Unqual` in your code doesn't do anything - the
type `M` remains `T`. Apparently, some strange t
I’d like to clarify: the original goal was to create an
alternative to Dub in order to make copies of arrays of objects
that contain copying constructors.
It's all good like this:
```d
unittest
{
T[] copyArray(T)(const T[] arr) if (!is(T == class)) {
T[] copy = new T[arr.length];
On Wednesday, 8 October 2025 at 07:46:32 UTC, Vindex9 wrote:
On Wednesday, 8 October 2025 at 02:58:15 UTC, Steven
Schveighoffer wrote:
My attempt:
```d
import std.traits;
inout(T)[] copyArray(T)(inout(T)[] arr) {
alias M = Unqual!T;
```
Unfortunately, `Unqual` in your code doesn't do an
Here's a slightly better solution. The lines will be copied.
```d
T[] copyArray(T)(inout(T)[] arr) {
T[] copy = new T[arr.length];
copy.length = arr.length;
static if (is(T == U[], U) && !is(T == immutable(Y)[], Y)) {
foreach(i, ref v; copy) {
v = copyArray(arr[i])
On Tuesday, 7 October 2025 at 18:43:18 UTC, Vindex9 wrote:
What am I doing wrong?
I want a safe copy: `const string[][] -> string[][]`.
I think the approach should be, make a copy, then cast the copy,
recurse if it's a nested array.
What you are likely running into is that D automatically s