in D Programming by
Q:

Why Aren't Function Parameters Const By Default?

1 Answer

0 votes
by
Since most (nearly all?) function parameters will not be modified, it would seem to make sense to make them all const by default, and one would have to specifically mark as mutable those that would be changed. The problems with this are:

1. It would be a huge break from past D practice, and practice in C, C++, Java, C#, etc.

2. It would require a new keyword, say mutable.

3. And worst, it would make declarations inconsistent:

4. void foo(int* p)

5. {

6.    int* q;

7.    ...

8. }

p points to const, and q points to mutable. This kind of inconsistency leads to all sorts of mistakes. It also makes it very hard to write generic code that deals with types.

Using in can mitigate the ugliness of having to annotate with const:

void str_replace(in char[] haystack, in char[] needle);

Related questions

0 votes
asked Mar 5 in D Programming by SakshiSharma
+1 vote
asked Mar 5 in D Programming by SakshiSharma
...