built-in int[] opSliceOpAssign throws?

monarch_dodra monarchdodra at gmail.com
Wed Sep 12 03:43:42 PDT 2012


On Wednesday, 12 September 2012 at 09:50:09 UTC, Namespace wrote:
> You are right, slice isn't nothrow, this should may be fixed.
> But if you don't assign your dynamic array first, you have a 
> problem: you cannot put elements in a empty dynamic array with 
> arr[i] = val;, you have to use arr ~= val;
>
> This code works:
>
> import std.stdio;
>
> nothrow void foo1(ref int[] a)
> {
>     foreach(i; 0..10)
>     {
>         a ~= 5;
>         a[i] += 5;
>     }
> }
>
> void foo2(ref int[] a) //10
> {
>     a[]  = 5;  //12, no explicit slice, so the whole array is 
> assigned with 5
>     a[] += 7;  //13
> }
>
> void main() {
> 	int[] a;
> 	
> 	foo1(a);
> 	foo2(a);
> 	
> 	writeln(a);
> }

I think I foun out what is going on: It is a problem with 
overlap. According to specs, overlapping arrays are illegal:

--------
void main()
{
   int[10] b;
   b[] = 5;

   b[0..6] += b[4..10]; //6
   b[0..6]  = b[4..10]; //7
}
--------

Here, Line 7 will produce an 
"object.Exception at src\rt\arraycat.d(40): overlapping array copy".
However, line 6 will produce nothing. Toying with it shows that 
it produces *unspecified* behavior.

So rephrasing my question in 2 questions:
1) Shouldn't "b[0..6] += b[4..10]" throw "something"?
2) Shouldn't the thrown object be an Error (and not an Exception)?


More information about the Digitalmars-d-learn mailing list