built-in int[] opSliceOpAssign throws?

Namespace rswhite4 at googlemail.com
Wed Sep 12 02:50:32 PDT 2012


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);
}


More information about the Digitalmars-d-learn mailing list