[Vala] List behavior with new operator



I've discovered something:  If you use a List object, be sure not to use the new operator followed by append.  It adds a null object to the list, that will cause you to get failed assertions when you iterate through the List.

I include some code to demonstrate the problem:

using GLib;

namespace Test {

        public class ListTest {

                public List<string> s = new List();
                public List<string> t;

                public listtest() {

                        s.append("S Number 1");
                        s.append("S Number 2");
                        s.append("S Number 3");
                        s.append("S Number 4");

                        t.append("T Number 1");
                        t.append("T Number 2");
                        t.append ("T Number 3");
                        t.append("T Number 4");
                }
        }

        static int main(string[] args) {

                ListTest l = new ListTest();

                int x=1;
                foreach(string s1 in l.s) {
                        stdout.printf("%d - %s\n",x++,s1);
                }
                stdout.printf("\n");
                x=1;
                foreach(string t1 in l.t) {
                        stdout.printf("%d - %s\n",x++,t1);
                }
        }
}

These are the results:

1 - (null)
2 - S Number 1
3 - S Number 2
4 - S Number 3
5 - S Number 4

1 - T Number 1
2 - T Number 2
3 - T Number 3
4 - T Number 4

I don't know if this is something fixable in Vala because it seems the result of the way GList is implemented in GLib, but maybe something can be done with the new operator so as not to actually create a null object.  I would image this behavior holds true for some of the other GLib container classes as well. 

At least it needs to be clearly documented since it is different from how the rest of the language works.

I do want to say thanks to Jurg and the rest of the developers for getting the foreach to work across all the containers.  Having to use Iterators in java is one of the few things I find annoying about Java. 


Thanks again and keep up the good work.  I'm having fun with vala, hope you are too.

Cayle
MIssouri, USA


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]