Best practices for logical const

Jesse Phillips Jesse.K.Phillips+D at gmail.com
Sat Feb 15 11:34:12 PST 2014


On Saturday, 15 February 2014 at 12:23:54 UTC, Peter Alexander 
wrote:
> If you want logical const in D, you just don't use const. If 
> you try to hack around it, it will just come back and bite you.
>
> As a result, when writing APIs, don't enforce constness of your 
> parameters if you require logical const. e.g. a range will not 
> necessarily have const front and empty. Trying to enforce that 
> will lead to trouble.

I fear it may not be that simple. Caching tends to be the main 
example for wanting logical const, but caching may not be the 
first thing implemented.

Write some code, make what you can const, build from that code, 
make more things const. Go back and implement caching, remove 
const from everything.

To me, the only requirement for implementing logical const is 
that you make sure it is never implemented on immutable. Casting 
away const is not undefined, only mutating it afterwards. But 
mutating a mutable address is still valid, so as long as _you_ 
guarantee the address is not immutable, no it won't format your 
harddrive.


More information about the Digitalmars-d mailing list