how does range.put work

Jos van Uden jvu at nospam.nl
Sat Aug 8 01:55:39 PDT 2009


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




More information about the Digitalmars-d-learn mailing list