GNOME Bugzilla – Bug 546440
Vala interface fields do not generate correct code
Last modified: 2018-05-22 13:09:06 UTC
I'm attaching an updated version of the interfaces.vala test case which adds an interface public field and then changes its value inside the class constructor: public abstract class Ibaz : Object { public class int public_field; } public class Baz : Object, Ibaz { class construct { public_field = 42; } } will print out a warning: ./testrunner.sh: line 42: 24833 Trace/breakpoint trap $VALAC -C --vapidir "$vapidir" --pkg gee-1.0 --basedir $topsrcdir -d $topbuilddir $testsrc.vala > $testbuild.err 2>&1 ERROR: Compiling ./interfaces.vala GLib-GObject-WARNING **: invalid cast from `ValaInterface' to `ValaClass' aborting... the resulting code does not cast the interface correctly inside the base_init().
Created attachment 115917 [details] [TEST] Interface field test case test case showing the interface public field error in the compiler.
Created attachment 115919 [details] result of the interfaces.vala compilation
it occurs to me that I might be completely off course, here. in short: if I can set class fields using the class constructor, how do I set interface fields? from IRC: < juergbi> ebassi: interface fields are not supported at all, yet, only class fields < juergbi> although they should be pretty similar to implement what I think should be needed is a way to override the interface_init function from Vala, like: public Ibaz () { public_field = 42; } which would be translated into: static void ibaz_iface_init (IbazIface *iface) { iface->public_field = 42; /* vtable fields are added here */ } or a: Ibaz constructor { } which would allow overriding the interface_init for multiple types.
Interface fields have not been implemented yet at all. We should be able to support them int he same way as class fields, though.
Meanwhile, valac emits the following error for your testcase: iface.vala:8.9-8.37: error: class fields are not supported in compact classes public class int public_field; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The actual case remains open though.
*** Bug 583598 has been marked as a duplicate of this bug. ***
Created attachment 197303 [details] [review] codegen: Initial support for class fields in interfaces Define the field in the struct and allow accessing the field from an instance context. Partially fixes bug 546440. What's next: 1) Private fields 2) Syntax for interface constructor (I like the proposed IBaz construct {}) or something like that 3) Access the field in static/class context
Vala as pointed out, have no support for all cases in C generated code. This a little special case, should be worked as enhancement, as pointed before. This feature is not blocked for a 1.0 release, because current solution cover mayor cases.
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/12.