Presence of constructor causes type deduced to be mutable

Ali Çehreli acehreli at yahoo.com
Mon Mar 4 16:18:23 PST 2013


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


More information about the Digitalmars-d-learn mailing list