After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 655594 - Static value stomped on by class constructor
Static value stomped on by class constructor
Status: RESOLVED OBSOLETE
Product: vala
Classification: Core
Component: Objects
0.13.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2011-07-29 19:18 UTC by Eric Gregory
Modified: 2018-05-22 14:06 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Example code (312 bytes, text/x-vala)
2011-07-29 19:18 UTC, Eric Gregory
Details

Description Eric Gregory 2011-07-29 19:18:29 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".
Comment 1 geert jordaens 2011-07-30 08:29:18 UTC
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.
Comment 2 Luca Bruno 2011-07-30 09:00:27 UTC
(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.
Comment 3 geert jordaens 2011-07-30 09:08:04 UTC
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.
Comment 4 Luca Bruno 2011-07-30 09:12:17 UTC
(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.
Comment 5 geert jordaens 2011-08-05 18:18:02 UTC
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.
Comment 6 Luca Bruno 2011-08-05 18:21:30 UTC
(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.
Comment 7 geert jordaens 2011-08-05 20:37:23 UTC
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 ();
}
Comment 8 GNOME Infrastructure Team 2018-05-22 14:06:33 UTC
-- 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.