GNOME Bugzilla – Bug 611904
static variables do not get initialized even after an object was created
Last modified: 2012-08-06 17:59:16 UTC
public class A { public static string A = "blafasel"; } does not generate any code in a_type_init() to initialize the static member variable from the (generated) constant struct.
It's static, Why do you want it in type initialization? Maybe you're looking for public class string A = "something";
Even a static variable needs to get initialized (= assigned some value) at some point - i guessed that static variables get set during type init - but maybe there is other, some better place to do this. Obviously some static variables can be directly initialized statically via C structs, but for some types that may not be possible, so ...
Maybe i should clarify the bug a little more: A.A never gets initialized (and thus always is null).
I'd actually like to move initialization of static members to something like module init functions in Dova (instead of type init functions as it's done in the GObject backend). This fixes issues when accessing static members before instantiating the corresponding instance and static members outside classes.
I am not sure what the advantage of that would be. On the other hand i see some difficulties if the vala compiler does not have all sources of a module available in a single run - or would that be a requirement then?
It's not quite clear yet, however, a module is not necessarily the same as a library or an application. You could have multiple modules in a single library. valac already needs access to all sources that have mutual dependencies as there is nothing like header files.
Another problem with statics - with the following code ... public class F { public static int i; public static int main(string[] args) { return F.i; } } ... the static variable only gets declared as extern (in the C code), thus the linker will not find it. Removing the 'public' keyword fixes the problem (declares i as static variable in the C code).
Would qualifying the static members and moving them to the global:: namespace be an option?
(In reply to comment #8) > Would qualifying the static members and moving them to the global:: namespace > be an option? It's the right workaround here, yes.
The Dova profile has been removed in Vala 0.17.4. The upcoming successor will be available from verbon.org when it's ready.