GNOME Bugzilla – Bug 701227
Type checking of generic parameters
Last modified: 2013-05-29 21:42:06 UTC
Created attachment 245596 [details] Full sample code We've been hitting a strange bug recently where generic type parameters aren't always checked, which can lead to all kinds of nasty runtime issues. Let's say we have two classes: > class TypeA { > public string foo = "foo"; > } > > class TypeB { > } and the following method: > void print_collection(Gee.Collection<TypeA> list) { > foreach(TypeA a in list) > stdout.printf("%s\n", a.foo); > } We set up a list of TypeBs: > Gee.List<TypeB> list = new Gee.ArrayList<TypeB>(); > list.add(new TypeB()); Now we print them: > print_collection(list); This, of course, creates undefined behavior at runtime. On my machine at least, I get the following output: > (null) I should mention that if I change both classes to use GObject, the runtime output is garbage characters. Still with me? Okay, now's the interesting part. > void print_list(Gee.List<TypeA> list) { > foreach(TypeA a in list) > stdout.printf("%s\n", a.foo); > } > /* snip */ > print_list(list); This generates the following error: > error: Argument 1: Cannot convert from `Gee.List<TypeB>?' to `Gee.List<TypeA>' Aside from the fact that the "list" variable isn't nullable, this error is more or less what I'd expect here. It's worth noting that List and Collection are both interfaces, so it seems strange that they'd be treated differently here. I've confirmed that his happens with both Gee 0.6 and 0.8, and with Vala 0.18 and 0.20.
Thanks for the bug report. This particular bug has already been reported into our bug tracking system, but please feel free to report any further bugs you find. *** This bug has been marked as a duplicate of bug 700142 ***