Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread Shriramana Sharma via Digitalmars-d-learn
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

Re: Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread Daniel Kozak via Digitalmars-d-learn
Shriramana Sharma píše v Pá 16. 10. 2015 v 16:05 +0530: > 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

Re: Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread Kagamin via Digitalmars-d-learn
On Friday, 16 October 2015 at 10:35:23 UTC, Shriramana Sharma wrote: 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)[]`? Use `inout` attribute for

Re: Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread Meta via Digitalmars-d-learn
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

Re: Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread Meta via Digitalmars-d-learn
On Friday, 16 October 2015 at 12:48:42 UTC, Meta wrote: This doesn't work for char because it has indirections (a pointer to its data). Whoops, should be char[], not char.

Re: Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread Mike Parker via Digitalmars-d-learn
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

Re: Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Oct 16, 2015 at 04:05:19PM +0530, Shriramana Sharma via Digitalmars-d-learn 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

Re: Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread anonymous via Digitalmars-d-learn
On Friday, October 16, 2015 12:35 PM, Shriramana Sharma wrote: > 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? immutable says that the data won't ever change. If references

Re: Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread Shriramana Sharma via Digitalmars-d-learn
Ali Çehreli wrote: http://ddili.org/ders/d.en/const_and_immutable.html#ix_const_and_immutable.parameter, %20const%20vs.%20immutable Hi Ali – I take this chance to personally thank you sincerely for your book which provides much-needed hand-holding in my baby D-steps. I did read that chapter

Re: Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/2015 07:02 PM, Shriramana Sharma wrote: > your book which provides I am glad that it is useful. :) > My question was however to the root of the issue, as to *why* the compiler > cannot consider mutable as immutable just like in C/C++ any non-const can be > taken as const.

Re: Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread Shriramana Sharma via Digitalmars-d-learn
Ali Çehreli wrote: > Actually, others gave the answer to that question, which was apparently > not very clear. :) Yes it was clear and I did understand it: and I posted a reply thanking the others too, but for some reason it was still sitting in my outbox... -- Shriramana Sharma, Penguin

Re: Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread Shriramana Sharma via Digitalmars-d-learn
Thanks all, for your kind explanations. Would then constString (for const(char)[]) and inoutString (for inout(char) []) be useful aliases if included in the runtime? -- Shriramana Sharma, Penguin #395953

Re: Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread Jakob Ovrum via Digitalmars-d-learn
On Saturday, 17 October 2015 at 02:03:01 UTC, Shriramana Sharma wrote: Ali Çehreli wrote: http://ddili.org/ders/d.en/const_and_immutable.html#ix_const_and_immutable.parameter, %20const%20vs.%20immutable Hi Ali – I take this chance to personally thank you sincerely for your book which

Re: Why can't function expecting immutable arg take mutable input?

2015-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/2015 03:35 AM, 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. :-( Welcome to the club! :) You can read my understanding of the issue at the following link: