Re: Unexpected behavior when casting away immutable

2015-09-23 Thread Dicebot via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 14:34:07 UTC, bachmeier wrote: I was not aware that you could "violate" immutable. In that case, it's not immutable. You can violate absolutely everything in a system language with casts and pointers. That is exactly what makes it system language. But you do

Re: Unexpected behavior when casting away immutable

2015-09-23 Thread John Colvin via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 14:34:07 UTC, bachmeier wrote: On Wednesday, 23 September 2015 at 05:24:05 UTC, John Colvin wrote: violating immutable is undefined behaviour, so the compiler is technically speaking free to assume it never happens. At the very least, neither snippet's resul

Re: Unexpected behavior when casting away immutable

2015-09-23 Thread bachmeier via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 05:24:05 UTC, John Colvin wrote: violating immutable is undefined behaviour, so the compiler is technically speaking free to assume it never happens. At the very least, neither snippet's result is guaranteed to show a change or not. At the most, literally an

Re: Unexpected behavior when casting away immutable

2015-09-23 Thread Mike Parker via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 11:38:38 UTC, Mafi wrote: On Wednesday, 23 September 2015 at 05:24:05 UTC, John Colvin wrote: On Wednesday, 23 September 2015 at 03:39:02 UTC, Mike Parker wrote: ... ``` immutable int x = 10; int* px = cast(int*)&x; *px = 9; writeln(x); ``` It prints 10, wh

Re: Unexpected behavior when casting away immutable

2015-09-23 Thread Mafi via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 05:24:05 UTC, John Colvin wrote: On Wednesday, 23 September 2015 at 03:39:02 UTC, Mike Parker wrote: ... ``` immutable int x = 10; int* px = cast(int*)&x; *px = 9; writeln(x); ``` It prints 10, where I expected 9. This is on Windows. I'm curious if anyone k

Re: Unexpected behavior when casting away immutable

2015-09-22 Thread John Colvin via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 03:39:02 UTC, Mike Parker wrote: I have a situation where I would like to demonstrate violating the contract of immutable (as an example of what not to do), but do so without using structs or classes, just basic types and pointers. The following snippet works

Re: Unexpected behavior when casting away immutable

2015-09-22 Thread Mike Parker via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 03:50:44 UTC, Vladimir Panteleev wrote: On Wednesday, 23 September 2015 at 03:39:02 UTC, Mike Parker wrote: ``` immutable int x = 10; int* px = cast(int*)&x; *px = 9; writeln(x); ``` It prints 10, where I expected 9. This is on Windows. I'm curious if anyone

Re: Unexpected behavior when casting away immutable

2015-09-22 Thread Vladimir Panteleev via Digitalmars-d-learn
On Wednesday, 23 September 2015 at 03:39:02 UTC, Mike Parker wrote: ``` immutable int x = 10; int* px = cast(int*)&x; *px = 9; writeln(x); ``` It prints 10, where I expected 9. This is on Windows. I'm curious if anyone knows why it happens. Essentially, because x is immutable, the compiler op

Unexpected behavior when casting away immutable

2015-09-22 Thread Mike Parker via Digitalmars-d-learn
I have a situation where I would like to demonstrate violating the contract of immutable (as an example of what not to do), but do so without using structs or classes, just basic types and pointers. The following snippet works as I would expect: ``` immutable int i = 10; immutable(int*) pi = &