www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - About immutable and const constructors

reply "FreeSlave" <freeslave93 gmail.com> writes:
Hello to all. I want to discuss immutable and const constructors. 
For a start let's write the simple class with two constructors:

class A
{
     int x;
     this() immutable
     {
         x = 42;
     }
     this()
     {
         x = 13;
     }
}

Well, dmd passes this code, but what if we want to create some 
object of class A?

void main()
{
     A a = new A;
}

hello.d|177|Error: constructor hello.A.this called with argument 
types:|
hello.d|177|Error: cannot implicitly convert expression (new A) 
of type immutable(A) to hello.A|

Class A has mutable constructor, but for some reason immutable 
one is called and then immutable object can not be converted to 
type A, which is mutable.

Let's rewrite this line to this:

immutable A a = new A;

hello.d|173|Error: constructor hello.A.this called with argument 
types:|

So what's the problem? Yes, both A's constructors really have 
empty argument lists, but... ah. Let's try another way:

immutable A a = new immutable A;

hello.d|173|Error: found 'A' when expecting '('|
hello.d|173|Error: basic type expected, not ;|
hello.d|173|Error: found ';' when expecting ')'|
hello.d|174|Error: semicolon expected, not '}'|

Again not. Looks like it's not correct statement at all. So I 
conclude there is no way to keep simultaneously both mutable and 
immutable constructors with same argument list. But as we saw 
above, compiler passes code if we don't create objects of class 
A, although such permission has no sense.

Well, now let's rewrite our class using const specifier instead 
of immutable

class A
{
     int x;
     this() const
     {
         x = 42;
     }
     this()
     {
         x = 13;
     }
}

void main()
{
     const A a = new A;
     assert(a.x == 42);
}

Project is built without errors or warning, but we get assertion 
failure in runtime. Error seems obvious: new A is constructed 
like mutable and only then assigned to object of class const A. 
Maybe the following code will be correct?

const A a = new const A;

hello.d|173|Error: found 'A' when expecting '('|
hello.d|173|Error: basic type expected, not ;|
hello.d|173|Error: found ';' when expecting ')'|
hello.d|174|Error: semicolon expected, not 'assert'|

But we get same errors as with immutable specifier.

I used dmd 2.060 and Code::Blocks on Debian GNU/Linux.
Sep 25 2012
parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
Ask in D.learn or in other forums. This forum is only a mirror of 
discussing issues from D bugzilla which is mistakenly opened for 
posting.
Sep 25 2012