Best practices for logical const
Jonathan M Davis
jmdavisProg at gmx.com
Sat Feb 15 22:32:53 PST 2014
On Sunday, February 16, 2014 04:35:52 Meta wrote:
> On Sunday, 16 February 2014 at 03:26:42 UTC, Steven Schveighoffer
>
> wrote:
> > I'm not sure what you are asking, because the answer I want to
> > give is so trivial :)
>
> Then I don't know what we're arguing about. I'm saying that if
> you pass mutable data to an inout function, inout effectively
> acts as logical const within that function, as the compiler
> doesn't allow you to modify the inout function argument but it is
> safe to cast away inout. Do you agree or disagree with this?
And that would be pointless. If a parameter is inout, then you have to treat
it as const and cannot possibly know that the argument is mutable unless it's
only ever passed a mutable argument and the programmer makes sure that that's
all it's ever passed. And if that's the case, what's the point of having it be
inout instead of mutable?
And you really shouldn't be thinking of inout as being different from const
from the function's perspective. The only difference is that if the caller
passes a mutable or immutable argument, then they get a return value with the
same mutability instead of it ending up as const like it would if the function
took const instead of inout. It makes no difference to the function itself,
and the function writer should not do anything differently inside the function
than they would do if it took const.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list