Re: [Vala] How to check if GLib.List is empty?



Hm, indeed. The following piece of code works fine:

int main(string[] argv)
{
    List<string> list = new List<string>();
    assert( list == null );
    return 0;
}


So this seems to be the answer to my question. But still there are two
weird things in my opinion:

   - Why I need to call operator new if the result value is still null?
   Can't I simply declare a reference List<string> list; and then call e.g.
   append()?
   - I thought that only references appended with question mark sign (e.g.
   List<string>?) can be null, cant' they?


BR, Vitaly Kirsanov
skype: vkirsan

2014-12-02 18:22 GMT+03:00 Luca Bruno <lethalman88 gmail com>:

On 02/12/2014 16:03, Виталий Кирсанов wrote:
Hello list.

Probably there is a well-known answer to this but I failed to find it.

Native C glib states that a null pointer GList is a valid list and it can
be used as an argument to various list operations such as
g_list_append().
And to check whether the list is empty you need just to compare the head
of
the list with NULL;

But it's not that straightforward with vala. If I write:

var list = List<string>();

then vala generates something like:

GList * list = NULL;


And after that I can call some of methods of list (e.g.
list.append("hello");) but call to list.data would lead to a crash.
That's
a bit strange but ok, I understand that functions able to operate null
data
values like GList are not very well mapped to vala where this reference
may
not be null.

The main question is when I have created list variable in vala, made some
insertions and deletions how do I check whether it is empty or not?
Probable answer would be I can call list.length(); method but it will not
be efficient if, say, I call it on every iteration of a loop and the list
is not empty.

Is there a more simple and efficient way?
== null

:)
_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list



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