Re: [Vala] How do you get from the generic, to the.....specific?
On Sun, Feb 19, 2012 at 5:32 AM, Matto Marjanovic wrote: > Are there any plans to add type bounds to Vala generics? > > This would allow something like (e.g., using Java's keyword): > > public int get_length (T val) { > return val.length; > } > > Yes, but not anytime soon. -- www.debian.org - The Universal Operating System ___ vala-list mailing list vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] How do you get from the generic, to the.....specific?
On 02/18/12 15:10, ant wrote: On 18 February 2012 21:56, Evan Nemerson wrote: public int get_length (T val) { if ( typeof (T) == typeof (string) ) { return ((string) val).length; } else { GLib.error ("Unable to handle type `%s'", typeof (T).name ()); } } Are there any plans to add type bounds to Vala generics? This would allow something like (e.g., using Java's keyword): public int get_length (T val) { return val.length; } I.e. the T is qualified to be a subclass of 'string' (not that this is necessarily possible in Vala), and thus the compiler knows that anything that can be done to a string can be done to an instance of T. -m ___ vala-list mailing list vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] How do you get from the generic, to the.....specific?
On 18 February 2012 21:56, Evan Nemerson wrote: Thanks for the comprehensive answer, it was most useful! > You seem to be operating under a pretty fundamental (though > understandable) misunderstanding about how Vala generics work. This > came up on stackoverflow recently, so you're not alone. Heh, you're not wrong there! > public int get_length (T val) { > if ( typeof (T) == typeof (string) ) { > return ((string) val).length; > } else { > GLib.error ("Unable to handle type `%s'", typeof (T).name ()); > } > } Thanks, that's exactly what I was after. cheers ant ___ vala-list mailing list vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
[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, 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 : 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(); wrapper.set_thing("test"); return 0; } cheers ant ___ vala-list mailing list vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list