Best practices for logical const
Meta
jared771 at gmail.com
Sat Feb 15 11:59:05 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.
inout is *sort of* logical const, if the underlying type is
immutable.
T logicalConst(T)(inout(T) t)
{
//Error: cannot modify inout expression t
t += 1;
return t;
}
void main()
{
auto n = 0;
auto m = logicalConst(m);
}
The only problem is figuring out if T itself is actually mutable
and not immutable or const. If you can do that, then it's fine to
cast inout away and mutate the value.
More information about the Digitalmars-d
mailing list