[Vala] How do you get from the generic, to the.....specific?



Apologies for my confusedf terminology. I'm meeting with repeated
abysmal failure trying to bend generics to my will.

Going back to basics, I started with the example code at
https://live.gnome.org/Vala/Tutorial#Generics.

I had to rename set_data() and get_data() to get this to compile, but
that's no deal.

My simple goal is this: having created a wrapper<string>, how do I
print it's length inside of set_thing(G data) ?? Or indeed access any
properties or methods of the specific 'G' thingy?

I can understand run-time errors if I instantiated a wrapper of some
non-gobject type, but the compile time error "the name 'get_type' does
not exist in the context of 'G'" has me baffled.

Here are my various attempts:


public class Wrapper<G> : Object {
    private G data;

    public void set_thing(G data) {
        this.data = data;

#if broken
        var x = data.length;  /* can't use String methods.... */
#endif

#if broken
        Type t = data.get_type(); /* can't use Object methods either... */
#endif

#if broken
        G x = data;               /* No */
        Type t = x.get_type();
#endif

#if broken
        var x = (G)data;          /* No */
        Type t = x.get_type();
#endif


#if segfault
        var x = data as Object; /* Works so far... */
        Type t = x.get_type();
        stdout.printf("x is %p\n", x); /* seg fault */
#endif

#if illegal
        var x = data as string; /* Operation not supported for type */
        Type t = x.length;
#endif

#if segfault
        var x = data as Object;
        int l;
        x.get("length", out l); /* seg fault */
#endif

    }

    public G get_thing() {
        return this.data;
    }
}

/********************************************************************************/

public static int main(string[] args) {

    var wrapper = new Wrapper<string>();
    wrapper.set_thing("test");


    return 0;
}



cheers
ant



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