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 13:36:13 PDT 2014


On 10/10/2014 9:28 AM, Yota wrote:
> Once again, I am thoroughly confused as to why the space is put before the * in
> a language where the * is associated with the type, and not the identifier.

In C, you can declare multiple variables like this:

     T *p, *q;

both are pointer to T. This is why conventionally the * gets put next to the 
symbol name. Such split up declarations aren't allowed in D, so conventionally 
the * gets put next to the type, as in:

     T* p, q;


> At which point I ask, why word it such that 'const' affects the symbol v, and
> not the type T*?  And why does the former syntax even exist if it is more proper
> to use the latter?

const as storage class is useful for doing things like putting data into a read 
only segment.

The const(T) type constructor syntax is to emphasize that const in D is 
transitive, i.e. it affects everything inside the ( ). This is not true of const 
in C++, which when used as a type constructor is left-associative, unless it is 
on the left when it becomes right-associative, which I find terribly confusing.


More information about the Digitalmars-d mailing list