www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Suggestion: arrays.

reply "DLearner" <bmqazwsx123 gmail.com> writes:
 From dlang:


Static array properties are: ... .dup Create a dynamic array of the same size and copy the contents of the array into it. .idup Create a dynamic array of the same size and copy the contents of the array into it. The copy is typed as being immutable. ... Dynamic array properties are: ... .dup Create a dynamic array of the same size and copy the contents of the array into it. .idup Create a dynamic array of the same size and copy the contents of the array into it. The copy is typed as being immutable. D 2.0 only ... <<< The problem: 'dup' is an abbreviation of 'duplicate'. However, for static arrays, result is not a true duplicate of the source. But, for dynamic arrays, result is a true duplicate. So the same abbreviation produces two different effects. Bugsource? Suggested solution: For static arrays, replace '.dup' with '.dyn' (and similarly for 'idup'). Use of 'dyn' would also add clarity.
Aug 11 2015
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Tuesday, 11 August 2015 at 08:59:50 UTC, DLearner wrote:
 From dlang:


Static array properties are: ... .dup Create a dynamic array of the same size and copy the contents of the array into it. .idup Create a dynamic array of the same size and copy the contents of the array into it. The copy is typed as being immutable. ... Dynamic array properties are: ... .dup Create a dynamic array of the same size and copy the contents of the array into it. .idup Create a dynamic array of the same size and copy the contents of the array into it. The copy is typed as being immutable. D 2.0 only ... <<< The problem: 'dup' is an abbreviation of 'duplicate'. However, for static arrays, result is not a true duplicate of the source. But, for dynamic arrays, result is a true duplicate. So the same abbreviation produces two different effects. Bugsource? Suggested solution: For static arrays, replace '.dup' with '.dyn' (and similarly for 'idup'). Use of 'dyn' would also add clarity.
2 choices: 1) break people's code and documentation by renaming .dup on static arrays 2) have 2 equivalent names for the same thing Neither is likely to happen. If you want it for your own code: T[] dyn(T, size_t n)(T[n] a) { return a.dup; }
Aug 11 2015
parent "jmh530" <john.michael.hall gmail.com> writes:
On Tuesday, 11 August 2015 at 09:08:57 UTC, John Colvin wrote:
 2 choices:
 1) break people's code and documentation by renaming .dup on 
 static arrays
 2) have 2 equivalent names for the same thing

 Neither is likely to happen.

 If you want it for your own code:

 T[] dyn(T, size_t n)(T[n] a)
 {
     return a.dup;
 }
The OP could also make it a property to make it even more similar. Perhaps the least amount of work would be if alias worked on functions (I don't see anything in the docs about that). The broader issue that it is very easy to mix up static and dynamic arrays remains.
Aug 11 2015