Type of generic type parameter for the struct is not saved.
It is possible to create and use generic structs in Vala, however information about the parameter exists only on compilation stage. While it works for basic read and write operations, it is not possible to use generic type to call other generic functions and/or create other generic objects.
Consider such vala code:
namespase Test {
public void print_type<T> (T obj) {
stdout.printf ("%s\n", typeof (T).name ());
}
public struct Foo<T> {
T data;
public Foo (T val) {
data = val;
}
public void print () {
print_type<T> (data);
}
}
public class EntryPoint {
public static void main (string[] args) {
Foo<int> test = Foo<int> (1);
test.print ();
}
}
Translation to C completes successfully, but the compilation to executable failes. The following code looks incorrect (unnecessary parts are ommited)
struct _TestFoo {
gpointer data;
};
void test_foo_print (TestFoo* self)
{
gconstpointer _tmp0_;
_tmp0_ = (*self).data;
test_print_type (self->priv->t_type, NULL, NULL, _tmp0_);
}
Tested on Fedora 37 with version from sources and version from repository
bash-5.2$ valac --version
Vala 0.57.0.146-94582
bash-5.2$ /bin/valac --version
Vala 0.56.3
Edited by Vladyslav Stovmanenko