The Artima Developer Community
Sponsored Link

Java Community News
What Features Would You Remove from Java?

63 replies on 5 pages. Most recent reply: Jun 12, 2007 12:07 PM by James Watson

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 63 replies on 5 pages [ « | 1 ... 2 3 4 5 ]
Mark Thornton

Posts: 275
Nickname: mthornton
Registered: Oct, 2005

Re: What Features Would You Remove from Java? Posted: Jun 12, 2007 6:38 AM
Reply to this message Reply
Advertisement
> >
> > > List<String> list = new ArrayList<String>();
> >
> > That example is fine but the following probably isn't:
> >
> > List li = new ArrayList();
> > li.add( 123 );
> > List<String> ls = li;
>
> Yes but the reference returned by a constructor is a
> special case where the warning could be omitted. In
> general, I am against these kinds of exceptions but this
> is one place where I would support it.

In other words why can't

List<String> list = new ArrayList();

work like

List<String> list = Collections.emptyList();

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: What Features Would You Remove from Java? Posted: Jun 12, 2007 9:00 AM
Reply to this message Reply
> In other words why can't
>
> List<String> list = new ArrayList();
>
> work like
>
> List<String> list = Collections.emptyList();

I think actually I'm really starting to questioning why it's necessary to provide a type to the constructor at all. I'm probably being naive or missing something but all generics checking is done through references. The generic type of the underlying object is irrelevant/illusionary. Why even require that the constructor 'receive' a generic type parameter?

This I think is what is so frustrating about Java generics. You are required to struggle to satisfy the compiler with all kinds of information that, in the end, it ignores. It seems pointless to require I pass my generic arguments to a constructor in order for it to do the exact same thing that it would do without them. I suppose this is done just in case reification is implemented later.

I looked up the old conversation and remembered why this is not allowed. If has to do something with the meaning of:

new ArrayList();

i.e. no parameters and nothing to infer the type from and backwards compatibility with raw types. There is something in the spec that the inferred type is Object when none is supplied and this means that existing code would automatically change from using raw types to parameterized types.

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: What Features Would You Remove from Java? Posted: Jun 12, 2007 11:07 AM
Reply to this message Reply
>The generic type of the underlying object is irrelevant/illusionary

That's an excellent way of expressing what I was trying to say! As I type the code I'm thinking "they use erasure, this is all an illusion, pay no attention to the man behind the curtain", yet I am required to go along with the illusion to make the compiler happy. It's jarring, and, if the types in the <> are long, tedious.

Maybe add static methods to the implementing types that mimic Collections.emptyList() in that they automatically fit the type? e.g.

List<Foo> fooList = ArrayList.new();

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: What Features Would You Remove from Java? Posted: Jun 12, 2007 12:07 PM
Reply to this message Reply
> Maybe add static methods to the implementing types that
> mimic Collections.emptyList() in that they automatically
> fit the type? e.g.
>
> List<Foo> fooList = ArrayList.new();

I don't think you can name the method new() but that's what was recommended to me as a work-around. The only concern I have with this is that it shouldn't replace the constructor unless you really want to use a factory method only and shouldn't modify the constructor.

Flat View: This topic has 63 replies on 5 pages [ « | 2  3  4  5 ]
Topic: John Ousterhout on Software Production Previous Topic   Next Topic Topic: Adobe Releases Flex 3, AIR, and Flash Betas

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use