digitalmars.D.learn - immutable ref foreach on values
- bearophile (12/12) Jun 22 2012 Is this expected and good?
- Tobias Pankrath (4/16) Jun 22 2012 I think it's good. In this special case, the compiler can see
- bearophile (4/7) Jun 22 2012 But aren't int implicitly castable to immutable?
- Dmitry Olshansky (10/14) Jun 22 2012 then every array is implicitly castable to tail immutable.
- bearophile (4/11) Jun 22 2012 I understand, thank you :-)
Is this expected and good? void main() { int[] array = [1, 2]; foreach (ref const(int) x; array) {} // OK foreach (ref immutable(int) x; array) {} // error } DMD 2.060alpha: temp.d(4): Error: argument type mismatch, int to ref immutable(int) Thank you, bye, bearophile
Jun 22 2012
On Friday, 22 June 2012 at 11:07:14 UTC, bearophile wrote:Is this expected and good? void main() { int[] array = [1, 2]; foreach (ref const(int) x; array) {} // OK foreach (ref immutable(int) x; array) {} // error } DMD 2.060alpha: temp.d(4): Error: argument type mismatch, int to ref immutable(int) Thank you, bye, bearophileI think it's good. In this special case, the compiler can see that you can't change array behind his back. That seems not to be true in general.
Jun 22 2012
Tobias Pankrath:I think it's good. In this special case, the compiler can see that you can't change array behind his back. That seems not to be true in general.But aren't int implicitly castable to immutable? Bye, bearophile
Jun 22 2012
On 22-Jun-12 16:16, bearophile wrote:Tobias Pankrath:then every array is implicitly castable to tail immutable. int[] array = [1, 2]; foreach (ref immutable(int) x; array) { ... func(arr); // arr is mutable, thus func can change x // so x can be at most const } -- Dmitry OlshanskyI think it's good. In this special case, the compiler can see that you can't change array behind his back. That seems not to be true in general.But aren't int implicitly castable to immutable?
Jun 22 2012
Dmitry Olshansky:then every array is implicitly castable to tail immutable. int[] array = [1, 2]; foreach (ref immutable(int) x; array) { ... func(arr); // arr is mutable, thus func can change x // so x can be at most const }I understand, thank you :-) Bye, bearophile
Jun 22 2012