Re: [Vala] Making a class iterable from VAPI bindings



On Sat, 2011-10-15 at 23:37 +0200, Luca Bruno wrote:
On Sat, Oct 15, 2011 at 10:47 PM, Calvin Walton <calvin walton kepstin ca>wrote:

Is there any way to add an iterator class to my .vapi file so I can use
foreach, or any plans to allow foreach on types which provide a length
property and get(index) method?


Call it size instead of length.

Ah, and like magic, the foreach is now accepted :) It would be nice to
have this documented at
http://live.gnome.org/Vala/Tutorial#Methods_With_Syntax_Support
along with the iterator-related stuff... So nice, in fact, that I just
added it myself.

Now I'm having some additional problems; the code

public static void test(AnnotationList a) {
        foreach (unowned Annotation b in a) {
                test2(b);
        }
}
public static void test2(Annotation a) {
}

Is failing to compile, with the error:
test.vala:5.35-5.36: error: duplicating AnnotationList instance, use unowned variable or explicitly invoke 
copy method
        foreach (unowned Annotation b in a) {
                                         ^^

This naturally does work if I do 
        foreach (unowned Annotation b in a.clone())
instead, but I'd prefer not to have to make a copy when it's not really
required...

The vapi extract:

[CCode (cheader_filename = "musicbrainz4/mb4_c.h")]
namespace Mb4 {
        [Compact]
        [CCode (free_function = "mb4_annotation_list_delete")]
        public class AnnotationList {
                public AnnotationList clone();
                public int get_count();
                public int get_offset();
                [CCode (cname = "mb4_annotation_list_item")]
                public unowned Annotation get(int item);
                public int size {
                        [CCode (cname = "mb4_annotation_list_size")]
                        get;
                }
        }
        [Compact]
        [CCode (free_function = "mb4_annotation_delete")]
        public class Annotation {
        }
}





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