www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Does opSlice work with immutable values?

reply "Joseph Cassman" <jc7919 outlook.com> writes:
I have a custom number type in the works here 
(https://github.com/joichiro/kuttaka/commit/a226d3368a64ae63b0c256
4f4a4ede375a5cee8). 
The test code for opSlice works in this commit. However, when I 
change the auto to immutable (on line 308) I get the following 
error (a similar error occurs for const):

src/bcd.d(309): Error: mutable method 
kuttaka.bcd.BcdSingle.opSlice is not callable using a immutable 
object
src/bcd.d(310): Error: mutable method 
kuttaka.bcd.BcdSingle.opSlice is not callable using a immutable 
object
src/bcd.d(311): Error: mutable method 
kuttaka.bcd.BcdSingle.opSlice is not callable using a immutable 
object
Failed: ["dmd", "-unittest", "-v", "-o-", "src/bcd.d", "-Isrc"]

How can I get immutable to work here? My thinking is that opSlice 
is creating a new object here so the sliced object should be 
allowed to be const or immutable.

I am using the current HEAD revision of the DMD compiler 
toolchain.

Joseph
Feb 23 2014
parent reply "anonymous" <anonymous example.com> writes:
On Monday, 24 February 2014 at 07:02:00 UTC, Joseph Cassman wrote:
 I have a custom number type in the works here 
 (https://github.com/joichiro/kuttaka/commit/a226d3368a64ae63b0c256
4f4a4ede375a5cee8). 
 The test code for opSlice works in this commit. However, when I 
 change the auto to immutable (on line 308) I get the following 
 error (a similar error occurs for const):

 src/bcd.d(309): Error: mutable method 
 kuttaka.bcd.BcdSingle.opSlice is not callable using a immutable 
 object
 src/bcd.d(310): Error: mutable method 
 kuttaka.bcd.BcdSingle.opSlice is not callable using a immutable 
 object
 src/bcd.d(311): Error: mutable method 
 kuttaka.bcd.BcdSingle.opSlice is not callable using a immutable 
 object
 Failed: ["dmd", "-unittest", "-v", "-o-", "src/bcd.d", "-Isrc"]

 How can I get immutable to work here? My thinking is that 
 opSlice is creating a new object here so the sliced object 
 should be allowed to be const or immutable.
Mark opSlice const then.
Feb 24 2014
parent reply "Joseph Cassman" <jc7919 outlook.com> writes:
On Monday, 24 February 2014 at 11:37:43 UTC, anonymous wrote:
 Mark opSlice const then.
That did it. Thanks. The const/immutable language spec page states that mutable and immutable are implicitly convertible to const. Never thought about this in the other direction though. And from what I understand, the const keyword added to a function makes the this pointer const. Can't see exactly why this works but it does. Anyways appreciate the help. Joseph
Feb 24 2014
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/24/2014 09:11 AM, Joseph Cassman wrote:

 the const keyword added to a [member] function makes the this pointer
 const. Can't see exactly why this works but it does.
const means "I will not modify data through this reference". Since there is not issue with modification, it can refer to mutable and immutable data. Ali P.S. It would be great if you could show the issue on a minimal code example. I tried to look at your original code but found it to be too long. Sorry... :)
Feb 24 2014
parent "Joseph Cassman" <jc7919 outlook.com> writes:
On Monday, 24 February 2014 at 17:24:18 UTC, Ali Çehreli wrote:
 On 02/24/2014 09:11 AM, Joseph Cassman wrote:

 the const keyword added to a [member] function makes the this
pointer
 const. Can't see exactly why this works but it does.
const means "I will not modify data through this reference". Since there is not issue with modification, it can refer to mutable and immutable data. Ali P.S. It would be great if you could show the issue on a minimal code example. I tried to look at your original code but found it to be too long. Sorry... :)
Thanks for the explanation. Yeah, wasn't sure whether the entire context would be better than providing a meaningless reduced code example. :P See what I can do. Joseph
Feb 24 2014
prev sibling parent "anonymous" <anonymous example.com> writes:
On Monday, 24 February 2014 at 17:11:20 UTC, Joseph Cassman wrote:
 The const/immutable language spec page states that mutable and 
 immutable are implicitly convertible to const. Never thought 
 about this in the other direction though. And from what I 
 understand, the const keyword added to a function makes the 
 this pointer const. Can't see exactly why this works but it 
 does.
Implicit conversion from immutable to const is exactly what's happening. It's not going the other direction. The this pointer is passed to the method via a hidden parameter. Qualifiers like const on methods really apply to that hidden this parameter. Then, when you call a const method on an immutable object, you really put an immutable object into a const parameter. And that's fine, because immutable implicitly converts to const. In code: struct S { void m() const {} } immutable S s; s.m(); is something like this behind the scenes: struct S {} void m(ref const S hidden_this) {} immutable S s; m(s);
Feb 24 2014