Notes on the Phobos style guide

dsimcha dsimcha at yahoo.com
Tue Aug 17 06:13:38 PDT 2010


== Quote from Kagamin (spam at here.lot)'s article
> bearophile Wrote:
> > > * In D, use of auto is recommended unless you want to make a
> > > specific point by mentioning the type.
> >
> > On the other hand code needs to be read too, sometimes by people that have not
written it. In this case seeing the actual types used is often better. So using
"auto" everywhere makes the code reading harder: if you aren't using an IDE that
tells you types, you sometimes need to follow the flux of the various calls until
you find what is the type, or sometimes you need to add temporary
writeln(typeof(x).stringof); inside the code to see what type it is. This problem
is common in dynamic languages.
> >
> I agree. Auto hinders understanding. Knowing that we have some fancy type here
gives you no knowledge about what's going on. Even with IDE. It also has issues
with interoperability. You don't want to pass fancy types to the external code, do
you?
> It also contradicts with the intention to have uniform style because this is an
intention to have understandable code.

IMHO anyone who doesn't like auto is trying to write Java in D.  D is **NOT** a
purely nominative typing language because because use of templates in idiomatic D,
especially D2, is so pervasive.  Even when I can't remember the type of something
and need it, there's always typeof(), which is sometimes clearer as well.  For
example, I can never remember what type regex() returns, so:

class Foo {
    // Declare a regex as a member variable w/o ever explicitly
    // knowing its type.
    typeof(regex("", "")) myRegex;
}

Maybe it's just my bias of despising nominative typing but I believe that, when
programming, if you're thinking at the level of nominative types, you're usually
thinking at too low a level.


More information about the Digitalmars-d mailing list