mutable reference to const object

Jonathan M Davis jmdavisProg at gmx.com
Tue Oct 12 02:32:04 PDT 2010


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


More information about the Digitalmars-d-learn mailing list