Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > abbreviated generic syntax

Reply
Thread Tools

abbreviated generic syntax

 
 
Roedy Green
Guest
Posts: n/a
 
      02-18-2013
On Mon, 18 Feb 2013 12:29:53 -0800 (PST), Lew <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>The diamond operator distinguishes the generics usage.


OK, that is obvious, but would it break any code?
--
Roedy Green Canadian Mind Products http://mindprod.com
The generation of random numbers is too important to be left to chance.
~ Robert R. Coveyou (born: 1915 died: 1996-02-19 at age: 80)
 
Reply With Quote
 
 
 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      02-19-2013
Roedy Green <(E-Mail Removed)> wrote:
> On Mon, 18 Feb 2013 12:29:53 -0800 (PST), Lew <(E-Mail Removed)>
> wrote, quoted or indirectly quoted someone who said :
>> The diamond operator distinguishes the generics usage.

> OK, that is obvious, but would it break any code?


I'm not taking the time to think it all through to the end,
but "fwiw":

To see any technical difference between the two patterns:
Base<Type> b = new Sub<>();
and
Base<Type> b = new Sub();

You might want to consider different subclasses of some class,
where one is itself generic and the other is not:
interface Base<T> { ... }
class Sub1<T> implements Base<T> { ... }
class Sub2 implements Base<String> { ... }

Most likely(*), it is currently *not* legal to write:
Base<String> b = new Sub2<>();

So, the compiler probably shouldn't *always* assume the <>
to a pattern like: Base<Type> b = new SubClass();

In how far it would be always possible for the compiler to
check the referenced class first (to see if it is generic
and if so then just assume the diamond), I can't tell.

PS:
*: I did not care enough to check the JLS or even try it.
 
Reply With Quote
 
 
 
 
Joshua Cranmer 🐧
Guest
Posts: n/a
 
      02-19-2013
On 2/18/2013 5:35 PM, Roedy Green wrote:
> On Mon, 18 Feb 2013 12:29:53 -0800 (PST), Lew <(E-Mail Removed)>
> wrote, quoted or indirectly quoted someone who said :
>
>> The diamond operator distinguishes the generics usage.

>
> OK, that is obvious, but would it break any code?


In the Java typing system, a generic type that is used without generics
(aka a raw type) is a very distinct type. "List" is not "List<?>" nor is
it "List<Object>" nor any other value that you stick in those brackets
[1]. If you didn't have the diamond operator, it would be ambiguous as
to whether inferred type arguments or the actual raw type was desired.
Imagine scenarios like:

Collections.singletonList(new List());

[1] This is *really* annoying because the type of List.class is
Class<List> (a rare type), not Class<List<?>>. If you thought raw types
were hard to use, rare types pretty much require you to sprinkle your
code with @SuppressWarnings.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
Arne Vajhoej
Guest
Posts: n/a
 
      02-19-2013
On 2/18/2013 6:35 PM, Roedy Green wrote:
> On Mon, 18 Feb 2013 12:29:53 -0800 (PST), Lew <(E-Mail Removed)>
> wrote, quoted or indirectly quoted someone who said :
>> The diamond operator distinguishes the generics usage.

>
> OK, that is obvious, but would it break any code?


I can not think of any.

But having a RHS expression which changes semantics after LHS is
not the Java way.

Arne


 
Reply With Quote
 
BGB
Guest
Posts: n/a
 
      02-19-2013
On 2/19/2013 11:45 AM, Arne Vajhoej wrote:
> On 2/18/2013 6:35 PM, Roedy Green wrote:
>> On Mon, 18 Feb 2013 12:29:53 -0800 (PST), Lew <(E-Mail Removed)>
>> wrote, quoted or indirectly quoted someone who said :
>>> The diamond operator distinguishes the generics usage.

>>
>> OK, that is obvious, but would it break any code?

>
> I can not think of any.
>
> But having a RHS expression which changes semantics after LHS is
> not the Java way.
>


yep, pretty much.


although taking the destination type into account could help with things
like preserving precision by promoting types early, or help avoid
unnecessary warnings/errors in a few other cases (for example, lessening
the need for suffixes on numerical constants, ...), it is less certain
in other cases, namely where the interpretation could have a potentially
significant influence on language semantics.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Abbreviated (DOS) name in titles Terry Pinnell Computer Support 10 11-10-2008 12:08 AM
XPath abbreviated form nils.grimsmo@gmail.com XML 4 06-25-2007 10:28 PM
abbreviated names in newsgroups Nuke Firefox 1 10-31-2004 12:51 AM
Can abbreviated printStackTrace() output be easily prevented for chained exceptions? Mike H Java 1 02-27-2004 04:09 PM
abbreviated newsgroups charlie Computer Support 2 08-30-2003 10:22 PM



Advertisments