What is the correct use of auto?

Unknown W. Brackets unknown at simplemachines.org
Sun Apr 13 14:42:54 PDT 2008


Why?  I'm not sure I see the relevance of your analogies to the point.

Surely no one shall die if they use auto.  Also, not knowing what 
something is (which can cause you no harm directly) is very different 
from not knowing where incoming danger might be.

I think you're trying to say that having autos in examples is like 
having mines, in that they are possible points of confusion to newcomers 
(who will not know what those things are.)  You may also be trying to 
say that using auto in examples teaches people to use auto more than 
they should.

However, I would say that auto should not be used, except in any case 
where the type auto is representing:

	- is unimportant to surrounding code (it is being paper-pushed.)
	- is unlikely to be used in any way other than the represented way 
(e.g. has no other useful methods than are already being called.)
	- is documented in some comment in the example.
	- is an example type, with no actual importance to the example (e.g. to 
be passed to a method that takes any value, which is what the example is 
showing.)

Here is an example which uses auto.  It is very simple and clear, and 
auto is making the example better.

// How to write to stderr
import std.stdio;

int main()
{
	// Most anything can be written using writefln().
	// If it's an object, it needs a toString() method.
	auto data = 42;

	// This outputs data (as a string) to stderr, and then a newline.
	writefln(stderr, "Look at this: %s", data);

	// You can also use writeln() for unformatted data.
	writefln(stderr, data);
}

Also, anytime you have:

auto something = new Something();

It's reasonable to assume the reader of the example will understand what 
something is, imho.  This is especially true with template examples, 
where someone may (additionally) be overwhelmed by the extraneous 
information in the declaration.

-[Unknown]


Georg Wrede wrote:
> THIS is something I've posted reams about, ever since auto became part 
> of D!
> 
> While auto might (and probably is) an everyday part of Good D Code, it 
> STILL has no place in examples.
> 
> (( An example: My 8 and 10 years old sons walked with me today. Neither 
> looked left nor right when we crossed the street. I told them (for the 
> 200th time) that, "hey, Daddy gets distracted with you guys fooling 
> around, and if Daddy misses a coming car, then we all die". Next 
> crossing, and I was the only one who even tried to look around.
> 
> OK, safety (like in buckling up (non-US: wearing safety belts in a 
> car)), has nothing to do with this comment. The only point here is, that 
> for people who don't *personally* "see" the point, no amount of nagging 
> seems to drive the issue through. And "auto" with D examples, seems to 
> be the same. Unfortunately. ))
> 
> So, please, never again a D code example with auto. Thank you all!!!



More information about the Digitalmars-d mailing list