GNOME Bugzilla – Bug 655594
Static value stomped on by class constructor
Last modified: 2018-05-22 14:06:33 UTC
Created attachment 192886 [details] Example code In a class with a static variable, the value of the variable is set to the default by the constructor. In the attached example, the program is expected to print out "i = 5". But it actually prints "i = -1".
IMHO, the behavior is correct. Test.init(); doesn't do anything useful, it is just a static function that doesn't return anything. The call to : Test t = new Test(); Just creates a new instance of Test so it is logic that the value is -1.
(In reply to comment #1) > IMHO, the behavior is correct. Test.init(); doesn't do anything useful, it is > just a static function that doesn't return anything. > > The call to : > Test t = new Test(); > > Just creates a new instance of Test so it is logic that the value is -1. It's not correct. That value must be 5. This bug won't be fixed anytime soon due to a general problem with mapping vala static to C static unfortunately.
Ok, So the expected behaviour is that the static class variable keeps the last value it is set to by in this case the static init function.
(In reply to comment #3) > Ok, So the expected behaviour is that the static class variable keeps the last > value it is set to by in this case the static init function. Yes the implementation trouble comes from static SomeObject obj = new SomeObject (); In this case we can't set it as C static initializer, thus we set it when the class is initialized (see class_init, the static constructor). See bug 611904, bug 589477, bug 543189 and maybe some other.
Is there really a general problem with mapping vala static to C static? Isn't the problem that the behaviour of the static keyword is not well defined in vala and therefore the mapping to c is not consistent.
(In reply to comment #5) > Is there really a general problem with mapping vala static to C static? > Isn't the problem that the behaviour of the static keyword is not well defined > in vala and therefore the mapping to c is not consistent. It's well defined but the problem is mapping to C.
I've read trough the documentation again: Could we translate : public class Test { private static int i = -1; } to : public class Test { private static int i { get; set; default = -1;} } Translate the private static into a global:: symbol. (I realize that below I'm mixing vala internal an c code but it's just as an example) And generate static getters/setters and static init. static gint test_get_i (void) { return global::test.i; } static void test_set_i (gint value) { global::test.i = value; } static void test_init_i () { global::test.i = -1; } And add the generated init func to the class init? static void test_class_init (TestClass * klass) { test_parent_class = g_type_class_peek_parent (klass); TEST_CLASS (klass)->finalize = test_finalize; test_init_i (); }
-- 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/217.