[Vala] Why do we keep static fields in GObject derived classes?
- From: Geert Jordaens <geert jordaens telenet be>
- To: vala-list gnome org
- Subject: [Vala] Why do we keep static fields in GObject derived classes?
- Date: Sun, 19 May 2013 08:57:06 +0200
Why do we keep static fields in GObject derived classes?
There is currently no way of correctly accessing static class members
unless at least one object is instantiated.
Moreover the initialisation of the fields depends on the variable type
(valuetype/reftype).
public class ClassA : Object {
public static int int_value_a = 7; // <=
Initialised in outside GObject class
public static string string_value_a = "7"; // <= Initialised
within the GObject class *_class_init
}
void main() {
stdout.printf("Access to static member ClassA.int_value_a ==
%d\n",ClassA.int_value_a);
stdout.printf("Access to static member ClassA.string_value_a ==
%s\n",ClassA.string_value_a);
var a = new ClassA();
stdout.printf("After first ClassA object instatiation\n");
stdout.printf("Access to static member ClassA.int_value_a ==
%d\n",ClassA.int_value_a);
stdout.printf("Access to static member ClassA.string_value_a ==
%s\n",ClassA.string_value_a);
}
=> output
Access to static member ClassA.int_value_a == 7
Access to static member ClassA.string_value_a == (null)
After first ClassA object instatiation
Access to static member ClassA.int_value_a == 7
Access to static member ClassA.string_value_a == 7
Could we define static fields as guaranteed initialized once class
fields? Keep the static modifier as a guard.
public class ClassA : Object {
public static int int_value_a = 7; // <=
Move field initializer within the GObject class *_class_init
public static string string_value_a = "7"; // <= Move field
initializer within the GObject class *_class_init (AS-IS)
/*
* Allow as field initializers are generated within the GObject
class *_class_init
*/
static construct {
int_value_a = 7;
string_value_a = "7";
}
/*
* Not allow access to static fields in a class construct block
* as field initializers are generated within the GObject class
*_base_init
* and are initialized on each first call of a Gobject Type /
Sub Type
*/
class construct {
/*
* Issue an Error or Waring for BC:
* Access to static member %s.%s in class construct
forbidden/discouradged
*/
int_value_a = 6;
string_value_a = "6";
}
/*
* Not allow access to static fields in a object construct block
* as field initializers are generated within the GObject class
*_constructor
* and are initialized on each instantiation of an object.
*/
construct {
/*
* Issue an Error or Waring for BC:
* Access to static member %s.%s in construct
forbidden/discouradged
*/
int_value_a = 5;
string_value_a = "5";
}
/*
* Not allow access in the standard object constructor
(guaranteed initialize once)
*/
public ClassA () {
/*
* Issue an Error or Waring for BC:
* Access to static member %s.%s in construct
forbidden/discouradged
*/
int_value_a = 4;
string_value_a = "4";
}
/*
* Allow access for a non standard constuctor that overrules
guaranteed initialize once
*/
public ClassA.x () {
int_value_a = 4;
string_value_a = "4";
}
}
I think this would make the behavior more defined.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]