Jos van Uden wrote: > Oliver wrote: >>> The source code for the standard library comes with the compiler. >>> If you look in std\array.d, you find this around line 279 (reflowed for >>> readability): >>>> void put(T, E)(ref T[] a, E e) { >>>> assert(a.length); >>>> a[0] = e; a = a[1 .. $]; >>>> } > > Would anybody care to explain what this is used for? I find > the example in array.d rather unhelpful. > > Example: > ---- > void main() > { > int[] a = [ 1, 2, 3 ]; > int[] b = a; > a.put(5); > assert(a == [ 2, 3 ]); > assert(b == [ 5, 2, 3 ]); > } > > You're putting an element in a, but then the first element is moved out > of a and the new one shows up in b? Weird. I guess I don't understand > what a range is. > > Jos
No; read the code. Before the put, a and b are pointing to the same span of memory. a.put(5) puts the value 5 into the front (first element) of the array, then advances the array. However, put can't "see" b, so it doesn't get updated along with a. The end result is that b = [5,2,3] and a = b[1..3] = [2,3]. Why do it like this? Here's an example: void putNumbers(Range)(Range r) { int i = 0; while( !r.empty ) { r.put(i); ++i; } } void main() { int[10] ten_numbers; putNumbers(ten_numbers); assert( ten_numbers = [0,1,2,3,4,5,6,7,8,9] ); } Note that putNumbers will work with any type that supports the range API, not just arrays.