Re: [Vala] static string variable is empty!

On Wed, Jan 6, 2010 at 7:28 AM, Jan Hudec <bulb ucw cz> wrote:

On Tue, January 5, 2010 13:12, Frederik wrote:
Non-value-type static class variables are only initialized after the
class was instantiated at least once. This behaviour is a little bit
counter-intuitive, and I hope it will change in the future.

I have to disappoint you -- that behaviour can't change.

The problem is, that the while the mechanism for static constructors
exists for C++, there does not seem to be a portable way to use it from
C (in gcc you can use the __attribute__((constructor)), but that's an

You have several options:

- create a throw-away instance:

  static int main (string[] args) {
      new Global ();
      stdout.printf ("all data is in: " + Global.dataDir);
      return 0;

Actually, there is no need to do that -- calling typeof(Global); is enough.

- call a static initialization method:

  static int main (string[] args) {
      Global.init ();
      stdout.printf ("all data is in: " + Global.dataDir);
      return 0;

This should not work. *static* methods do not cause a class to be
initialized (*class* methods do, though).

- make 'dataDir' const, if it is not intended to change

- make the variable a class one instead of static one.

public class Global {
   class string dataDir = "whatever";

To me to correct way is to use:
    public static const string dataDir = "/usr/local/share/";
instead of:
    public static string dataDir = "/usr/local/share/";

Also please follow the Vala coding conventions (data_dir instead of dataDir)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]