Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Joshua Cranmer Newsgroups: comp.lang.java.programmer Subject: Re: diamond operator Date: Thu, 05 Apr 2012 18:18:00 -0500 Organization: A noiseless patient Spider Lines: 67 Message-ID: References: <8404504.2717.1333650899408.JavaMail.geo-discussion-forums@pbrx1> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 5 Apr 2012 23:18:05 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="WpcHJSul77m+zlbR9GVqkA"; logging-data="7521"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19XsNaqPtTDW51MhrvvsOYCAXT8cfbq1+Q=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20120327 Thunderbird/11.0.1 In-Reply-To: <8404504.2717.1333650899408.JavaMail.geo-discussion-forums@pbrx1> Cancel-Lock: sha1:pTd+bVeJmq2wG7KCpt6S9uRugPc= Xref: csiph.com comp.lang.java.programmer:13426 On 4/5/2012 1:34 PM, Lew wrote: > Arivald wrote: >> It will be better to provide "auto" type detector, like in C++. For >> example, instead of: Map> map = new >> HashMap>(); use: auto map = new >> HashMap>(); >> >> ...and "map" variable will be resolved at compile time to >> HashMap>. >> >> This allow very handy construct, like: >> >> auto data = SomeService.getProviders(); > > If 'auto' is a type, it should begin with an upper-case letter. If > it's not, you should explain what you intend. The `auto' comes from C++11, where the type of the variable is defined to be the type of the static value being assigned to it [1]. It is a lot more powerful in C++, where the language can define some obscene, opaque hidden types whose exact signatures are left unspecified by the specification. The largest set of these are the STL iterators, where iterators look like `std::map::iterator', but, when expanded, tend to be a class with five or six template parameters and a name with way too many underscores to be sane. The following all refer to the same type: std::map::iterator std::_Rb_tree, std::_Select1st >, std::less, std::allocator > >::iterator std::_Rb_tree_iterator > Given that A and B can be very long types themselves, it quickly becomes obscene just to write a simple for each loop, although C++11 also introduces a version of for-each loops that ameliorate this. The other reason for auto is lambda expressions: the type of a lambda expression is "a unique, unnamed class that has the following properties", much as the actual type of an anonymous inner class in Java is a unique, unnamed class which is only guaranteed to inherit from some method. > This idea is incompatible with the current direction of type > inference, which is in the other direction, and with Java's strong > typing philosophy, and apparently the Powers That Be disagreed that > your idea is better, as they didn't implement it. The C++11 auto keyword is still fully strongly typed. It's meaning is best described is "the compiler already knows the precise type of this variable, and I know what I can do with this type, but I don't want to spend 15 minutes fighting the compiler to figure out what the right type actually is." In C++, because of the magic of templates and typedefs, you can actually spend several minutes just trying to figure out how to tell the compiler the proper name of a type. In short, auto is not a matter of dynamic versus static typing, it's a matter of implicit versus explicit typing. C++ has a fairly complex type system that has lots of edge cases which happen surprisingly often, while Java's type system is cleaner, even if you throw in generics. Given also that I suspect templates are much more abused than Java's generics, I don't think the auto is as useful in Java as it is in C++. [1] If you really want to play language lawyer, it's the same type for the initializer expression as would be derived for template argument inference. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth