Make const, immutable, inout, and shared illegal as function attributes on the left-hand side of a function

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Fri Oct 10 14:10:16 PDT 2014


On 10/10/2014 11:11 AM, Brad Anderson wrote:
> left-hand const is primarily a problem for new users of the language
> (particularly the large amount coming from C++). These users aren't running
> linters, they are still just trying to get basic projects off the ground. This
> issue is one of the top things I see new users have problems with in the D IRC
> channel. You can find new users having problems with it on Stack Overflow too.

Const works differently in D than in C++, and this doesn't change that. First off,

     const int foo();

returning const(int) is pointless. More likely, someone coming from C++ might write:

     const T *foo(); // in C++ returns pointer to const T

expecting it to be:

     const(T)* foo();  // D way to return pointer to const T

and:

     const T *p;  // C++: pointer to const char
                  // D: const pointer to const char

which means different things in C++ and D. Fortunately, nearly all these issues 
quickly run afoul of the compiler's type checker, and can be as quickly 
corrected. C++ doesn't have a notion of transitive const, so the C++ syntax 
cannot be generally applied and have it mean the same thing.

At some point, the new D user needs to spend a bit of time learning the const 
system and unlearning the C++ one.


More information about the Digitalmars-d mailing list