I was testing dmd 2.065 with some old work of mine and came upon this bug. I've tried my best to reduce it to this (first time ever I post a bug report, sorry if I don't do it the right way):
struct BufferedArray(/*of*/T) {
T[] m_array;
uint index=0;
this( T[] array ) { m_array = array; }
}
void push(T)( T Element, /*on*/BufferedArray!(T) array ) {
array.m_array[array.index] = Element;
array.index++;
}
unittest {
auto test_array = BufferedArray!(/*of*/string)( new string[10] );
push( "Hello", test_array );
push( "world", test_array );
push( "!", test_array );
assert( test_array.index == 3 ); //fails
}
As the bug was particulary unclear to me, I inserted a bunch of writeln's which clearly show that index is updated inside the push function but as soon as push exits, index is zero again. The field is not actually updated.
Comment #1 by destructionator — 2014-03-13T20:03:05Z
You should take the buffered array as ref in the push function. Otherwise, it is passed as value, so it updates in the function, but those changes aren't preserved outside the function.
So it isn't really a bug, though it might have worked a few years ago because D1 array slices were a weird value/reference hybrid. (Well, they still are a hybrid, but they aren't so weird anymore and consistently act like a value when appending; see: http://dlang.org/d-array-article.html )
Comment #2 by lionel.thiry — 2014-03-13T20:41:35Z
Thanks for your fast answer. struct are passed by value, how did I forget that? So many years without programming, probably... I suppose I should close this issue. Sorry for the noise.