www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - DMD1 function template broken or did I f.u.?

reply 0ffh <frank youknow.what.todo.interNETz> writes:
Hi, all!

Try this:

---< snip >---

void remove(T)(out T[] array,T element) {
   int r=0,w=0;
   while (r<array.length) {
     if (array[r]!=element)
       array[w++]=array[r];
     ++r;
   }
   array.length=w;
}

void test() {
   int[] array;
   int element=2;
   //
   array=[1,3,2,2,1,3,1,1,2];
   writef("direct\n");
   writef("  before : ",array,"\n");
   int r=0,w=0;
   while (r<array.length) {
     if (array[r]!=element)
       array[w++]=array[r];
     ++r;
   }
   array.length=w;
   writef("  after  : ",array,"\n");
   //
   array=[1,3,2,2,1,3,1,1,2];
   writef("template\n");
   writef("  before : ",array,"\n");
   remove!(int)(array,element);
   writef("  after  : ",array,"\n");
}

---< snap >---

I get the following output:

direct
   before : [1,3,2,2,1,3,1,1,2]
   after  : [1,3,1,3,1,1]
template
   before : [1,3,2,2,1,3,1,1,2]
   after  : []

So, my question is: Huh?

Kind regards.
Aug 24 2010
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 24 Aug 2010 11:18:35 -0400, 0ffh  
<frank youknow.what.todo.internetz> wrote:

 Hi, all!

 Try this:

 ---< snip >---

 void remove(T)(out T[] array,T element) {
    int r=0,w=0;
    while (r<array.length) {
      if (array[r]!=element)
        array[w++]=array[r];
      ++r;
    }
    array.length=w;
 }

 void test() {
    int[] array;
    int element=2;
    //
    array=[1,3,2,2,1,3,1,1,2];
    writef("direct\n");
    writef("  before : ",array,"\n");
    int r=0,w=0;
    while (r<array.length) {
      if (array[r]!=element)
        array[w++]=array[r];
      ++r;
    }
    array.length=w;
    writef("  after  : ",array,"\n");
    //
    array=[1,3,2,2,1,3,1,1,2];
    writef("template\n");
    writef("  before : ",array,"\n");
    remove!(int)(array,element);
    writef("  after  : ",array,"\n");
 }

 ---< snap >---

 I get the following output:

 direct
    before : [1,3,2,2,1,3,1,1,2]
    after  : [1,3,1,3,1,1]
 template
    before : [1,3,2,2,1,3,1,1,2]
    after  : []

 So, my question is: Huh?
s/out/ref out means "return this argument by reference, but initialize it to its initial value first" which for arrays means, a null array. ref means "pass the argument by reference." Also, btw, you should not need to specifically call the !int version, you can just do remove(array, element). -Steve
Aug 24 2010
prev sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
0ffh <frank youknow.what.todo.internetz> wrote:

 So, my question is: Huh?
The answer to this should for symmetry be: Duh! However, it is not quite that simple.
 void remove(T)(out T[] array,T element) {
This is the line that gives you problems. You are expecting 'out' to work like 'ref', which it doesn't. From [1]: "out parameters are set to the default initializer for the type of it." Also, this newsgroup is for automated messages from D's Bugzilla. You might want to ask this kind of questions in digitalmars.D.learn in the future. [1]: http://digitalmars.com/d/2.0/function.html#parameters -- Simen
Aug 24 2010