Re: If I understand const right...

2016-03-23 Thread ag0aep6g via Digitalmars-d-learn
On 23.03.2016 22:26, ag0aep6g wrote: On 23.03.2016 22:18, cy wrote: On Wednesday, 23 March 2016 at 21:10:49 UTC, ag0aep6g wrote: [...] b = new int(*b + 1); Here "b" is pointing to mutable heap allocated data, which got cast to constant. with b = b + 1, it's still constant memory. It's

Re: If I understand const right...

2016-03-23 Thread ag0aep6g via Digitalmars-d-learn
On 23.03.2016 22:18, cy wrote: On Wednesday, 23 March 2016 at 21:10:49 UTC, ag0aep6g wrote: [...] b = new int(*b + 1); Here "b" is pointing to mutable heap allocated data, which got cast to constant. with b = b + 1, it's still constant memory. It's stack memory. Its constness isn't any

Re: If I understand const right...

2016-03-23 Thread cy via Digitalmars-d-learn
On Wednesday, 23 March 2016 at 21:10:49 UTC, ag0aep6g wrote: Just to be 100% clear: you're adding to the pointer here, No, that's what I meant to do. b = new int(*b + 1); Here "b" is pointing to mutable heap allocated data, which got cast to constant. with b = b + 1, it's still constant

Re: If I understand const right...

2016-03-23 Thread ag0aep6g via Digitalmars-d-learn
On 23.03.2016 21:52, cy wrote: const(int)[2] a = [23,24]; const(int)* b = a; Should be: const(int)* b = a.ptr; writeln(," always constant"); writeln(a, " always constant"); There's some subtlety here. `a` itself is not const, but its elements are. `a` being a fixed-sized array, you can't

If I understand const right...

2016-03-23 Thread cy via Digitalmars-d-learn
a = a + 1 a is const, a + 1 is const, yet a can't be assigned to a + 1. And I think the reason is like... const(int) a = 23; while(something()) { a = a + 1; } in the first iteration, a is set to 23, and the value of "a + 1" is 24, but where is the computer gonna store that 24? It can't