Re: [Vala] Arrays in generics



On 05/03/2014 20:36, James Dean Palmer wrote:
Hi, I'm new to Vala and have run into an issue with generics and arrays.
  Consider this example:

public class Vector<T> {
     internal T[] items;
     ....
     public Vector.from(T[] source) {
     ....
     }
}

This allows me to conveniently initialize the vector like this:

new Vector<string>.from({"Fred", "Daphne", "Velma"})

But the problem arises that if I do the same thing with integers:

new Vector<int>.from({1, 2, 3})

Vala doesn't complain but the c compiler does because the generic is
representing the array as a gpointer* where outside the generic the array
is gint*.  Since the size of the types is different I can't correctly
access an integer array from within a generic nor can I correctly pass a
gpointer* back to a gint*.  So while it compiles, the results are wrong
because when I use the array it is moving across the array at the wrong
stride.  To illustrate my point, this works correctly because the size is
compatible on my system:

new Vector<long>.from({1l, 2l, 3l})

Is there anything I can do to check the type or size of T in the generic to
provide custom type/size specific initialization?  Or is there some other
technique I might employ other than creating non-generic containers
(IntVector, BoolVector, etc..)?
You can check the type like typeof(T) == typeof(int)


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