Tidy auto [Was: Re: @disable]

dsimcha dsimcha at yahoo.com
Sun Jan 17 07:10:07 PST 2010


== Quote from bearophile (bearophileHUGS at lycos.com)'s article
> This discussion clearly shows that the current semantics of the "auto" is not
clean enough and it needs to be improved.
> To improve it D needs to adopt the strategy of using all attributes in a more
tidy and semantically clean way, as Java does. Java doesn't compile "useless" or
wrong attributes as D sometimes does.
> So it can be better to this to be a compilation error:
> enum x = 10;
> And accept only:
> auto enum x = 10;
> Or:
> int enum x = 10;
> Such tidy enforcement of attributes can be seen as fussy and sometimes it
requires extra compilations to remove all the mistakes, but it helps the
programmer understand in less time the semantics of those attributes. In pedagogy
it's well known that when a student is learning something, it's much better if the
rules are enforced in a strict way at the beginning.
> And I'd like in D2 the "override" to be always necessary (even when the code is
not compiled with "-w") as in C#, to avoid other bugs. I can show an example of
such possible bugs.
> Bye,
> bearophile

What would this accomplish?  Everyone who's been using D for a while knows that,
if some other qualifier that proves to the compiler that you're declaring a
variable is in your declaration, the variable is `auto` unless you explicitly
declare it some other type.  It works perfectly, and immutable et al. have the
same semantics:

immutable x = 3.14;  // x is an immutable value of inferred type.
immutable real x = 3.14;  // x is explicitly a real.
const y = 1;  // y is a const value of inferred type.
const uint y = 1;  // y is a const uint.

Furthermore, I think that less verbosity encourages good practices.  I've gotten
into the habit of declaring all my stack variables immutable when writing a
function, unless I really need them to be mutable.  This makes code a lot easier
to understand because, when I look at the function later and try to figure out how
it works, I know right off the bat that only a small subset of the variables are
ever modified after they're declared.  If doing this were more verbose, i.e. if I
couldn't just write:

immutable y = 2 * x + 1;

I might be less inclined to do this.



More information about the Digitalmars-d mailing list