GNOME Bugzilla – Bug 568972
Generics: Can't return generic array
Last modified: 2010-02-08 07:29:35 UTC
This code doesn't compile: ---------------------------------------------------------- public class Generic<G> : Object { public G[] get_array () { return new G[10]; } } void main () { var generic = new Generic<int> (); int[] array = generic.get_array (); } ---------------------------------------------------------- generic.vala:10.8-10.34: error: Assignment: Cannot convert from `G[]' to `int[]?' int[] array = generic.get_array(); ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Compilation failed: 1 error(s), 0 warning(s)
Still reproducible with 0.7.5. And extremely annoying because the Gee.AbstractCollection<G>.to_array() is exactly this case. Casting to the appropriate array type seems to be a usable workaround.
*** Bug 588825 has been marked as a duplicate of this bug. ***
Created attachment 152120 [details] [review] support generic arrays This patch simply expand the generic usage in the array. This solves other two boring problems: - say a method void m<T> (T[] arr), the call to m<int> ({"string"}) was allowed, now it's not possible and this is more sane. - errors won't say "Cannot convert from something to T[]" which was meaningless, but the message contains the actual type.
Created attachment 152121 [details] [review] support generic arrays was wrong patch sorry :S (git commit -a is evil)
commit 1788fa5854ac3e393978039851b3a173adc642ec Author: Luca Bruno <lethalman88@gmail.com> Date: Sun Jan 24 01:27:42 2010 +0100 Fix generic type resolution for arrays of generic types Arrays of generic types are still only supported for pointer-based element types. Fixes bug 568972.
*** Bug 523909 has been marked as a duplicate of this bug. ***