Re: [Vala] GLib.List error



On Mon, Dec 28, 2009 at 13:37:00 +0000, xiaohu wrote:
i have below code:

          var icon_list = new List<Pixbuf> (); 
          int[] sizes = { 16, 32, 48, 64 };

          foreach (var size in sizes)
            {   
              var pixbuf = icon_theme.load_icon ("eshare", size, 0); 
              icon_list.append (pixbuf);
            }   
          set_default_icon_list (icon_list);

compile above code got below error:

 error: `GLib.List' does not have a default constructor

the error was generate by first line:
  var icon_list = new List<Pixbuf> ();

i use vala 0.7.8, glib-2.22.3, gtk+-2.18.6

Short answer: You don't want to use GLib.List, you want to use Gee.List.

(Unless some existing API requires GLib.List, of course.)

Long answer:

Glib.List is a wrapper for GList and that is really not a list, but rather
a list item, with nothing representing the list as a whole. Therefore you
can't create a list -- and you don't have to, because that's just the
variable. Note, that due to it's funny properties, you can't easily assign
the list to another variable, so it's pretty useless except for passing to
Glib and Gtk methods that expect it (most of them expect SList, though).

To use it, you actually start with a null-initialized variable and call the
.prepend method on that variable *and* reset the variable to the result of
the call!  Yes, it's called on null the first time, but that is indeed what
the method expects!

Note, that I am saying .prepend(). NEVER USE .append ON A GLib.List!!!!!!!!
(that's because while the list is doubly-linked, it is not circular and all
you have is pointer to the begining, so append means iterating it, while
prepend is trivial).

If you indeed need to give the list to an existing API, I belive your sample
should have been:

    List<Pixbuf> icon_list = null;
    int[] sizes = { 64, 48, 32, 16 }; // if the order matters - uses prepend
    
    foreach (var size in sizes)
      {
        var pixbuf = icon_theme.load_icon ("eshare", size, 0);
        icon_list = icon_list.prepend (pixbuf); // must assign back!!!
      }
    set_default_icon_list ((owned)icon_list); // transfer ownership!!!

Lists are non-copyable, so there can be only one owner at a time and the
setter probably wants to take ownership (it's more efficient, as most of the
time the caller won't need the list anymore). If it does not, the (owned)
cast won't be there.

-- 
                                                 Jan 'Bulb' Hudec <bulb ucw cz>



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