Re: Copying array with const correctness

2025-10-18 Thread Vindex9 via Digitalmars-d-learn
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 ~=

Re: Copying array with const correctness

2025-10-18 Thread Vindex9 via Digitalmars-d-learn
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

Copying array with const correctness

2025-10-18 Thread Vindex9 via Digitalmars-d-learn
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

Re: Copying array with const correctness

2025-10-18 Thread Vindex9 via Digitalmars-d-learn
On Tuesday, 7 October 2025 at 19:07:28 UTC, Vindex9 wrote: alternative to Dub * alternative to dup property

Re: Copying array with const correctness

2025-10-17 Thread Vindex9 via Digitalmars-d-learn
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

Re: Copying array with const correctness

2025-10-17 Thread Vindex9 via Digitalmars-d-learn
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];

Re: Copying array with const correctness

2025-10-08 Thread Steven Schveighoffer via Digitalmars-d-learn
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

Re: Copying array with const correctness

2025-10-08 Thread Vindex9 via Digitalmars-d-learn
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])

Re: Copying array with const correctness

2025-10-07 Thread Steven Schveighoffer via Digitalmars-d-learn
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