Cannot create generic property of type string
Vala version: 0.56.3
This may be linked to #564, but I'm not 100% on that so I'm opening this separately just in case. While working on a larger piece of software, I discovered that classes implementing an interface with a generic property can't use string
as their generic type.
Here's a minimal repro:
interface IFoo<T> : Object {
public abstract T data { get; set; }
}
class Foo : Object, IFoo<string> {
public string data { get; set; }
}
void main () {
var foo = new Foo ();
foo.data = "foo";
assert (foo.data == "foo");
}
Here is the full build output generated:
from /usr/include/glib-2.0/glib/gasyncqueue.h:34,
from /usr/include/glib-2.0/glib.h:34,
from /usr/include/glib-2.0/gobject/gbinding.h:30,
from /usr/include/glib-2.0/glib-object.h:24,
from /home/df458/test.vala.c:4:
/home/df458/test.vala.c: In function ‘ifoo_get_type’:
/usr/include/glib-2.0/glib/gatomic.h:131:5: warning: argument 2 of ‘__atomic_load’ discards ‘volatile’ qualifier [-Wincompatible-pointer-types]
131 | __atomic_load (gapg_temp_atomic, &gapg_temp_newval, __ATOMIC_SEQ_CST); \
| ^~~~~~~~~~~~~
/usr/include/glib-2.0/glib/gthread.h:262:7: note: in expansion of macro ‘g_atomic_pointer_get’
262 | (!g_atomic_pointer_get (location) && \
| ^~~~~~~~~~~~~~~~~~~~
/home/df458/test.vala.c:140:13: note: in expansion of macro ‘g_once_init_enter’
140 | if (g_once_init_enter (&ifoo_type_id__once)) {
| ^~~~~~~~~~~~~~~~~
/home/df458/test.vala.c: In function ‘foo_real_set_data’:
/home/df458/test.vala.c:187:19: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
187 | old_value = foo_real_get_data (base);
| ^
/home/df458/test.vala.c: In function ‘foo_ifoo_interface_init’:
/home/df458/test.vala.c:214:25: warning: assignment to ‘const void * (*)(IFoo *)’ {aka ‘const void * (*)(struct _IFoo *)’} from incompatible pointer type ‘const gchar * (*)(IFoo *)’ {aka ‘const char * (*)(struct _IFoo *)’} [-Wincompatible-pointer-types]
214 | iface->get_data = foo_real_get_data;
| ^
/home/df458/test.vala.c:215:25: warning: assignment to ‘void (*)(IFoo *, const void *)’ {aka ‘void (*)(struct _IFoo *, const void *)’} from incompatible pointer type ‘void (*)(IFoo *, const gchar *)’ {aka ‘void (*)(struct _IFoo *, const char *)’} [-Wincompatible-pointer-types]
215 | iface->set_data = foo_real_set_data;
| ^
/home/df458/test.vala.c: In function ‘foo_get_type’:
/usr/include/glib-2.0/glib/gatomic.h:131:5: warning: argument 2 of ‘__atomic_load’ discards ‘volatile’ qualifier [-Wincompatible-pointer-types]
131 | __atomic_load (gapg_temp_atomic, &gapg_temp_newval, __ATOMIC_SEQ_CST); \
| ^~~~~~~~~~~~~
/usr/include/glib-2.0/glib/gthread.h:262:7: note: in expansion of macro ‘g_atomic_pointer_get’
262 | (!g_atomic_pointer_get (location) && \
| ^~~~~~~~~~~~~~~~~~~~
/home/df458/test.vala.c:250:13: note: in expansion of macro ‘g_once_init_enter’
250 | if (g_once_init_enter (&foo_type_id__once)) {
The code compiles, but at runtime this error is generated when setting a value to the property:
GLib-GObject-CRITICAL **: Read/writable property 'data' on class 'Foo' has type 'gchararray' which is not exactly equal to the type 'gpointer' of the property on the interface 'IFoo'
I did a quick test swapping string
for int
and got roughly the same result. I suspect any type that doesn't result in a pointer in the generated C code (any non-object I guess?) will trigger this, but my experience with the deeper parts of Vala is quite limited.