Re: Why =?UTF-8?B?4oCcbmV34oCdPw==?=
On 02/04/2011 07:53 PM, Lawrence D'Oliveiro wrote:
Java copied C++ in using the word ???new??? to indicate class instantiation on
the heap. (Of course, Java is different in not having any other kind of
class instantiation.)
But what purpose does this word serve? Java defines no meaning for
???classname(args)??? different from ???new classname(args)???. How many hundreds of
times a day do Java programms write something like
classname varname = new classname(args);
compared to, dare I say it, Python:
varname = classname(args)
I???m surprised Java never added a shorter form, say
classname varname(args);
which is of course straight out of C++.
First off: `new' is a good visual indicator when quickly scanning code.
Saving keystrokes is not all that important--if it were, then you'd end
up with wonderfully terse programming languages like APL. I don't care
if I can write a matrix multiplication in a single line of code, I won't
be able to understand it 6 months later.
The Python way of doing things makes sense only because classes are
basically glorified functions. The same syntax in Java is syntactically
identical to a function call--so now you get syntactic ambiguity you
resolve at semantic time. Oh joy [1].
The last form shares the problem I mentioned earlier--it fails to stand
out suitably. There is also the minor point that it only is really
usable during variable declaration. The C++ form only makes sense as an
extension of the variable declaration, particularly about the
distinction between stack- and heap-storage variables.
To make the long story short: the current syntax isn't particularly
verbose (until you get generics), and shorter versions have problem.
[1] One point to make: `a b(c);' is a very syntactically ambiguous
phrase in C++. It can be either a function prototype declaration or a
declaration of a variable b of type a with an arg c. C++ has been,
justifiably in my opinion, accused of being very difficult to parse.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth