const int vs. int const

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Fri Aug 15 14:08:08 PDT 2014


On Friday, 15 August 2014 at 18:47:49 UTC, Walter Bright wrote:
> It is not ambiguous from a semantic or syntactic point of view, 
> but it appears to be ambiguous for those coming from C++. This 
> was discussed at length a few years ago, but no solution 
> emerged that didn't make things much worse.

I still think that we'd be far better off if all attributes which 
could apply to a function's return type were illegal on the 
left-hand side of the function. All allowing it on the left does 
is cause confusion and bugs. It's already best practice to put it 
those attributes on the right, because if you don't, everyone who 
reads the code ends up asking whether the attribute was supposed 
to apply to the return type, and the parens were forgotten, or 
whether it was supposed to apply to the function.

It was my understanding that the whole reason that attributes 
like const or immutable are allowed on the left-hand side is 
because all of the attributes are allowed on both sides, and it 
would be inconsistent to not allow const on the left. But as it 
turns out, several aren't allowed on the right - 
https://issues.dlang.org/show_bug.cgi?id=12930 - making what we 
have already inconsistent - and since it's bad practice to put it 
on the left anyway, I think that it's well worth it to have the 
inconsistency of not allowing const, immutable, inout, or shared 
on the left-hand side. It would cost us pretty much nothing and 
will just avoid confusion and bugs. And IMHO, the fact that we're 
_already_ inconsistent with where the attributes can go makes it 
so that the argument that const should be allowed on the left for 
constistency's sake isn't particularly valid.

https://issues.dlang.org/show_bug.cgi?id=12931

- Jonathan M Davis


More information about the Digitalmars-d mailing list