www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - mutable reference to const object

reply Benjamin Thaut <code benjamin-thaut.de> writes:
I want to have a mutable reference to a const object. So that I can 
reasign the reference but can not modifiy the object. Is this possible 
in D 2.0? I found tailconst in the const FAQ but as it is not 
implemented in D 2.0 I'm clueless how to realize this.

-- 
Kind Regards
Benjamin Thaut
Oct 12 2010
next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Tue, 12 Oct 2010 13:07:59 +0400, Benjamin Thaut  
<code benjamin-thaut.de> wrote:

 I want to have a mutable reference to a const object. So that I can  
 reasign the reference but can not modifiy the object. Is this possible  
 in D 2.0? I found tailconst in the const FAQ but as it is not  
 implemented in D 2.0 I'm clueless how to realize this.
For pointers: const(int)* c = new int(); c = new int(); // reassign For references: import std.typecons; Rebindable!(Object) o = new Object(); o = new Object();
Oct 12 2010
prev sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Tuesday 12 October 2010 02:07:59 Benjamin Thaut wrote:
 I want to have a mutable reference to a const object. So that I can
 reasign the reference but can not modifiy the object. Is this possible
 in D 2.0? I found tailconst in the const FAQ but as it is not
 implemented in D 2.0 I'm clueless how to realize this.
Use Rebindable!() in std.typecons. It's a struct which can hold a const or immutable object. When you want to reassign it, you merely assign it the new reference and a new struct is created to replace it. Unfortunately, at this point, tail const has pretty much been decided to be too difficult implement and/or too difficult to do cleanly, so it's not in D and likely never will be. If it's mentioned in the online docs, the docs need to be updated. It's been a long time since there was any kind of tail const in D. - Jonathan M Davis
Oct 12 2010
parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
Jonathan M Davis Wrote:

 Unfortunately, at this point, tail const has pretty much been decided to be
too 
 difficult implement and/or too difficult to do cleanly, so it's not in D and
likely 
 never will be. If it's mentioned in the online docs, the docs need to be 
 updated. It's been a long time since there was any kind of tail const in D.
 
 - Jonathan M Davis
What I have heard is that it can't be done syntactically for references. As Denis shows you can do it with points (and arrays) but not object references. There are many syntax suggestion threads so I suggest search for them before bring it up again.
Oct 12 2010
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Tuesday, October 12, 2010 15:27:41 Jesse Phillips wrote:
 Jonathan M Davis Wrote:
 Unfortunately, at this point, tail const has pretty much been decided to
 be too difficult implement and/or too difficult to do cleanly, so it's
 not in D and likely never will be. If it's mentioned in the online docs,
 the docs need to be updated. It's been a long time since there was any
 kind of tail const in D.
 
 - Jonathan M Davis
What I have heard is that it can't be done syntactically for references. As Denis shows you can do it with points (and arrays) but not object references. There are many syntax suggestion threads so I suggest search for them before bring it up again.
Yes, you can do it with pointers, but not with references, and it causes problems for stuff like ranges. There have been several discussions on it over time, and Walter has pretty much said that it's not a feature that's going to happen. He spent too much time on it before without success to want to try at it again. - Jonathan M Davis
Oct 12 2010
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 12 Oct 2010 18:54:49 -0400, Jonathan M Davis <jmdavisProg gmx.com>  
wrote:

 On Tuesday, October 12, 2010 15:27:41 Jesse Phillips wrote:
 Jonathan M Davis Wrote:
 Unfortunately, at this point, tail const has pretty much been decided  
to
 be too difficult implement and/or too difficult to do cleanly, so it's
 not in D and likely never will be. If it's mentioned in the online  
docs,
 the docs need to be updated. It's been a long time since there was any
 kind of tail const in D.

 - Jonathan M Davis
What I have heard is that it can't be done syntactically for references. As Denis shows you can do it with points (and arrays) but not object references. There are many syntax suggestion threads so I suggest search for them before bring it up again.
Yes, you can do it with pointers, but not with references, and it causes problems for stuff like ranges. There have been several discussions on it over time, and Walter has pretty much said that it's not a feature that's going to happen. He spent too much time on it before without success to want to try at it again.
If someone were to do the work, and the syntax was bearable, it might be considered. I think Walter no longer wants to accept any ideas without proof that they will work -- he's already worked enough on trying to implement tail-const. I personally think tail const is going to be a very important feature that will not be solvable via the library. Tail-const ranges are going to be very important when it comes to things like collections. -Steve
Oct 14 2010