On Friday, 16 October 2015 at 10:35:23 UTC, Shriramana Sharma
wrote:
Hello. I still haven't wrapped my mind around the
const/immutable thing yet and am still stuck in C/C++ mode. :-(
A function that takes mutable arguments cannot be called with
immutable input at the call site since it does not promise to
*not* mutate the input. That's of course clear.
Why can't a function that takes an immutable argument be called
with a mutable input at the call site?
IOW, why isn't mutable implicitly convertible to immutable?
I just finished writing a string processing module which calls
multiple subroutines, and all of them carrying arguments with
type `string` viz. `immutable(char)[]` IIUC, and I tried to
pass it something which came from File.byLine(), then got the
error:
function textattr.applyTextAttr (string text) is not callable
using argument types (char[])
I understand that const can refer to either mutable or
immutable, so does this mean I should replace all occurrences
of `string` in arguments and return values of functions by
`const(char)[]`?
This actually *is* possible, if the type you're passing is a
value type with no indirections. Then you can just pass it by
value and it will be implicitly convertible to immutable. This
doesn't work for char because it has indirections (a pointer to
its data).