Vala supports a limited form of generics. It’s a shame, coming from a Java background not to see the thorough implementation that I am used to. Vala coming from a C# background might have inherited it’s form of generics, but that sadly was not possible, and with good reason. Vala maps onto an existing type system, whereas C# generics was a revolution in the underlying type system.
What’s the point? Well, it’s easy to break. Not that you should be trying, mind, but you should be aware of some gotchas in the polymorphism.
class AContainer {
string value;
}
class A1Container : AContainer {
string value1;
}
class A2Container : AContainer {
string value2;
}
void add_something(List<AContainer> list) {
list.add(new A1Container());
}
void main() {
add_something(new List<AContainer>()); // Good
add_something(new List<A1Container>()); // Good
add_something(new List<A2Container>()); // Bad!
}
I have typed this from memory, so please forgive the odd mistake, but it gets the idea across. It is perfectly possible to, within the rules of the language, put the wrong type of object into the container.
If some other peice of code that depends on a List<A1Container> having exactly the expected types, with the expected visible fields, is executed, we can very easily corrupt memory and/or crash the program.
Like I say, this is just something to look out for. After all, we’re real programmers aren’t we!