www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How do I instantiate a class as invariant?

reply "Craig Black" <craigblack2 cox.net> writes:
I can't figure out how to instantiate a class as invariant.  The following 
code issues this error:  cannot implicitly convert expression (new A) of 
type hello.A to invariant(A)

class A {}

int main(char[][] args)
{
  invariant A a = new A;
  return 0;
}

-Craig 
Apr 02 2008
parent Walter Bright <newshound1 digitalmars.com> writes:
Craig Black wrote:
 I can't figure out how to instantiate a class as invariant.  The 
 following code issues this error:  cannot implicitly convert expression 
 (new A) of type hello.A to invariant(A)
You'll have to do a cast at some point. This isn't the most robust way as the compiler won't be able to check if it really is invariant, and we are working on some ideas to improve it, but that's the way it is at the moment.
Apr 02 2008