www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Presence of constructor causes type deduced to be mutable

reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
I am losing my mind! :)

What do you expect the type of 's' be?

struct S
{
     int[] arr;

     this(int[] arr)
     {
         this.arr = arr;
     }
}

void main()
{
     auto arr = [ 1 ];
     auto s = immutable(S)(arr);    // What is the type of s here?
}

I thought that it would be immutable(S). Didn't we have those 
discussions about 'auto' meaning more "automatic type deduction" these 
days other than "automatic storage duration"? I remember that it used to 
be the case. Not so with dmd 2.062:

     assert(is (typeof(s) == S));    // Passes
     s.arr[0] = 3;                   // Compiles

The interesting thing is, the presence of the constructor is the cause 
of this. Remove the constructor and the type of 's' becomes immutable(S) 
and the code cannot be compiled as it shouldn't be:

     auto s = immutable(S)(arr);

Error: cannot implicitly convert expression (arr) of type int[] to 
immutable(int[])

Then I try adding an immutable constructor:

     this(int[] arr)              // Existing
     {
         this.arr = arr;
     }

     this(int[] arr) immutable    // Added
     {
         // ...
     }

Error: constructor deneme.S.this called with argument types:
	((int[]) immutable)
matches both:
[...]

Is this a known issue?

Ali
Mar 04 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
Ali Çehreli:

 Is this a known issue?
I don't know, I have added it: http://d.puremagic.com/issues/show_bug.cgi?id=9647 Bye, bearophile
Mar 04 2013