www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Global invariants?

reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
Hi,

I often find that I'd like to have some sort of 'global invariant', i.e. 
an invariant at module scope (or, more generally, a static invariant). 
So, for example:

private __gshared Mutex lock;

invariant()
{
     assert(lock);
}

shared static this()
{
     lock = new typeof(lock)();
}

This would be at module level, and the invariant would be called the 
same way it is for member methods, just only in static (free) functions. 
This could of course be generalized to classes and structs.

Is this a good idea?

-- 
- Alex
May 12 2012
parent deadalnix <deadalnix gmail.com> writes:
Le 12/05/2012 21:37, Alex Rønne Petersen a écrit :
 Hi,

 I often find that I'd like to have some sort of 'global invariant', i.e.
 an invariant at module scope (or, more generally, a static invariant).
 So, for example:

 private __gshared Mutex lock;

 invariant()
 {
 assert(lock);
 }

 shared static this()
 {
 lock = new typeof(lock)();
 }

 This would be at module level, and the invariant would be called the
 same way it is for member methods, just only in static (free) functions.
 This could of course be generalized to classes and structs.

 Is this a good idea?

This look awesome !
May 12 2012