March 05, 2013 Presence of constructor causes type deduced to be mutable | ||||
---|---|---|---|---|
| ||||
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 |
March 05, 2013 Re: Presence of constructor causes type deduced to be mutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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 |
Copyright © 1999-2021 by the D Language Foundation