Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   diamond operator (http://www.velocityreviews.com/forums/t945088-diamond-operator.html)

Roedy Green 04-04-2012 01:54 AM

diamond operator
 
I was looking over the table of contents of a book about new features
in Java 7 and came across a reference to the "diamond operator".

It turns out to be simple, but one of the precious bits of Java
syntactic sugar to make it terser.

http://fahdshariff.blogspot.ca/2011/...-operator.html

http://mindprod.com/book/9781849685627.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
When you were a child, if you did your own experiment
to see if it was better to put to cocoa into your cup first
or the hot milk first, then you likely have the programmer gene..

Arne Vajhøj 04-04-2012 02:06 AM

Re: diamond operator
 
On 4/3/2012 9:54 PM, Roedy Green wrote:
> I was looking over the table of contents of a book about new features
> in Java 7 and came across a reference to the "diamond operator".
>
> It turns out to be simple, but one of the precious bits of Java
> syntactic sugar to make it terser.
>
> http://fahdshariff.blogspot.ca/2011/...-operator.html


It is listed in the release notes:
http://www.oracle.com/technetwork/ja...es-418459.html
that link to the docs:

http://docs.oracle.com/javase/7/docs...-creation.html

It was probably also discussed here last summer when
Java 7 was released.

Arne

Silvio Bierman 04-04-2012 11:12 AM

Re: diamond operator
 
On 04/04/2012 03:54 AM, Roedy Green wrote:
> I was looking over the table of contents of a book about new features
> in Java 7 and came across a reference to the "diamond operator".
>
> It turns out to be simple, but one of the precious bits of Java
> syntactic sugar to make it terser.
>
> http://fahdshariff.blogspot.ca/2011/...-operator.html
>
> http://mindprod.com/book/9781849685627.html


With some exaggeration one might call it Java's type inferencing...

David Lamb 04-04-2012 12:23 PM

Re: diamond operator
 
On 04/04/2012 7:12 AM, Silvio Bierman wrote:
> On 04/04/2012 03:54 AM, Roedy Green wrote:
>> I was looking over the table of contents of a book about new features
>> in Java 7 and came across a reference to the "diamond operator".

> With some exaggeration one might call it Java's type inferencing...


Well, maybe, but from the little I recall of my programming language
design classes, "real" type inferencing is a lot more powerful, so I
think you ought to have said "much exaggeration."

Silvio Bierman 04-04-2012 01:40 PM

Re: diamond operator
 
On 04/04/2012 02:23 PM, David Lamb wrote:
> On 04/04/2012 7:12 AM, Silvio Bierman wrote:
>> On 04/04/2012 03:54 AM, Roedy Green wrote:
>>> I was looking over the table of contents of a book about new features
>>> in Java 7 and came across a reference to the "diamond operator".

>> With some exaggeration one might call it Java's type inferencing...

>
> Well, maybe, but from the little I recall of my programming language
> design classes, "real" type inferencing is a lot more powerful, so I
> think you ought to have said "much exaggeration."


Yep, it was an understatement.

Jim Janney 04-04-2012 04:23 PM

Re: diamond operator
 
Roedy Green <see_website@mindprod.com.invalid> writes:

> I was looking over the table of contents of a book about new features
> in Java 7 and came across a reference to the "diamond operator".
>
> It turns out to be simple, but one of the precious bits of Java
> syntactic sugar to make it terser.
>
> http://fahdshariff.blogspot.ca/2011/...-operator.html
>
> http://mindprod.com/book/9781849685627.html


This isn't wrong, but it fixes a problem I never knew I had. Any
competent IDE will expand that for you anyway.

--
Jim Janney


Arne Vajhøj 04-04-2012 11:41 PM

Re: diamond operator
 
On 4/4/2012 8:23 AM, David Lamb wrote:
> On 04/04/2012 7:12 AM, Silvio Bierman wrote:
>> On 04/04/2012 03:54 AM, Roedy Green wrote:
>>> I was looking over the table of contents of a book about new features
>>> in Java 7 and came across a reference to the "diamond operator".

>> With some exaggeration one might call it Java's type inferencing...

>
> Well, maybe, but from the little I recall of my programming language
> design classes, "real" type inferencing is a lot more powerful, so I
> think you ought to have said "much exaggeration."


Give that the JLS says:

<quote>
If the type argument list to the class is empty — the diamond form "<>"
— the type arguments of the class are inferred.
</quote>

then the usage of "type inference" can be defended.

But it is certainly not what type inference is in languages
like Scala etc..

Arne


Lew 04-05-2012 01:15 AM

Re: diamond operator
 
Jim Janney wrote:
> Roedy Green writes:
>> I was looking over the table of contents of a book about new features
>> in Java 7 and came across a reference to the "diamond operator".
>>
>> It turns out to be simple, but one of the precious bits of Java
>> syntactic sugar to make it terser.
>>
>> http://fahdshariff.blogspot.ca/2011/...-operator.html
>>
>> http://mindprod.com/book/9781849685627.html

>
> This isn't wrong, but it fixes a problem I never knew I had. Any
> competent IDE will expand that for you anyway.


It was added to Java to satisfy the whiners who were afraid they might strain a finger typing "Ctrl-V" or hitting ENTER on the IDE suggestion. Heaven help any of those folks should they ever have to actually work for a living..

Not that I eschew the diamond operator myself now that it's here, but I wasn't clamoring for it beforehand.

--
Lew
Diamonds are a girl's best friend. Dogs are a man's best friend. Diamonds cost more at first, but don't require feeding, walking, shots, neutering or spaying, veterinary or funeral bills, housebreaking, orreplacement expensesfor broken goods, and don't have litters. Still, I wish I had a dog.

Arivald 04-05-2012 08:11 AM

Re: diamond operator
 
W dniu 2012-04-04 03:54, Roedy Green pisze:
> I was looking over the table of contents of a book about new features
> in Java 7 and came across a reference to the "diamond operator".
>
> It turns out to be simple, but one of the precious bits of Java
> syntactic sugar to make it terser.
>
> http://fahdshariff.blogspot.ca/2011/...-operator.html
>
> http://mindprod.com/book/9781849685627.html


It will be better to provide "auto" type detector, like in C++.
For example, instead of:
Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
use:
auto map = new HashMap<Integer, List<String>>();

....and "map" variable will be resolved at compile time to
HashMap<Integer, List<String>>.

This allow very handy construct, like:

auto data = SomeService.getProviders();

In this case type of "data" will be deducted from return type of
SomeService.getProviders().

This will save lot of typing. And save refactoring time in case if
getProviders return type changes. And will auto-adapt to changes in
libraries.

--
Arivald

David Lamb 04-05-2012 12:03 PM

Re: diamond operator
 
On 04/04/2012 7:41 PM, Arne Vajhøj wrote:
> But it is certainly not what type inference is in languages
> like Scala etc..

Let alone Standard ML, where IIRC type inference is NP-hard.



All times are GMT. The time now is 09:28 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.