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 621944 - construct does not invoke before accessing static member
construct does not invoke before accessing static member
Status: RESOLVED DUPLICATE of bug 543189
Product: vala
Classification: Core
Component: Code Generator
0.8.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2010-06-17 22:39 UTC by Saggi Mizrahi
Modified: 2010-06-19 10:01 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Saggi Mizrahi 2010-06-17 22:39:56 UTC
I know that the documentation states that static construct happens before the first instantiation of the class. But you can't really use it to initialize static members because they might be accessed before the first instantiation.

the singleton design pattern is the perfect example where it could have bee useful to use a static struct but is impossible.

Take this example:

class Singleton : Object {
    public Singleton instance {get; private set;}
    static construct {
        instance = Singleton();
    }
    private Singleton() {};
}

void main(){
    var singleton = Singleton.instance;
}

This doesn't work because construct never happens. I think giving *real* static constructor (initialized before main or on first access to any member of class or derived classes) is an important feature and also the current behavior is a bit confusing.
Comment 1 Luca Bruno 2010-06-18 20:13:46 UTC
Looks the same as bug 543189
Comment 2 Saggi Mizrahi 2010-06-19 10:01:49 UTC
It is, sorry about that. Marking as duplicate

*** This bug has been marked as a duplicate of bug 543189 ***