Proposal: Type inference for variable definition/initialization using the 'auto' keyword.

Lawrence Kesteloot lk at teamten.com
Fri May 15 21:27:13 PDT 2009


Reinier Zwitserloot wrote:
> It should be optional. And it will be, if you add the 'final varName =
> expression;' syntax to java. Nobody is forcing you to use it

Nobody is forcing me to use it, but unfortunately no one can stop my
co-workers from using it either.

> Perhaps someone can enlighten me why using an expression more than
> once requires you to explicitly type it,

I like the added documentation. If an expression (as you call it) is
going to be used many times in a method, the it's probably important
and I want to immediately know its type. When I see:

    final params = getX().getY().getZ();

I don't want to have to chase down the return value of getX() so I can
look up its getY() method, then that object's getZ() method, so I can
figure out the type of "params". You might argue that I already don't
explicitly know the type of, say, the "getX().getY()" sub-expression,
but it's only used in passing and I don't really care. What I need to
know is, "What can I do with params?"

Reading code is hard enough without forcing this extra work on the
reader. And writing the type isn't much of a burden for the author,
since he already knows it.

I don't particularly buy the argument that "the IDE can just tell
you." I'm not sure we want to turn Java into a language that's only
usable in a small set of editing environments.

Lawrence



More information about the coin-dev mailing list