Why can't function expecting immutable arg take mutable input?
Shriramana Sharma via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Oct 16 03:35:19 PDT 2015
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)[]`?
--
Shriramana Sharma, Penguin #395953
More information about the Digitalmars-d-learn
mailing list