Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old May 28th, 2007, 06:35 PM
jupiter
Guest
 
Posts: n/a
Default Generics references

I've noticed that the type in generic declarations is not
"inherited" by new references to the object. This seems
inconsistent to me but maybe there's a good reason for it. To wit:

ArrayList<Stringlist1 = new ArrayList<String>();

ArrayList list2 = list;

list1.add(new Integer(3)); //compiler catches it as expected.
list2.add(new Integer(3)); //compiler is fat, dumb and happy.

I thought the idea of a reference was to point to the original
object and pick up all of the relevant data. Why not type?







  #2  
Old May 29th, 2007, 06:05 PM
Mark Rafn
Guest
 
Posts: n/a
Default Re: Generics references

jupiter <jupiter49byebyeSpam@msn.comwrote:
Quote:
>I've noticed that the type in generic declarations is not
>"inherited" by new references to the object. This seems
>inconsistent to me but maybe there's a good reason for it.
Type inference is intentionally weaker than it could be. There are good
reasons for it, I suspect, but it annoys me sometimes.

To wit:
Quote:
>ArrayList<Stringlist1 = new ArrayList<String>();
>ArrayList list2 = list;
List2 is explicitly NOT declared as a parameterized type. It happens to have
an ArrayList<Stringin it now, but you could assign it an ArrayList<Object>
later without error.
Quote:
>list1.add(new Integer(3)); //compiler catches it as expected.
>list2.add(new Integer(3)); //compiler is fat, dumb and happy.
Right, because you told it to be.
Quote:
>I thought the idea of a reference was to point to the original
>object and pick up all of the relevant data. Why not type?
Type is tricky. It's both a property of the variable and a property of the
referent. The object type can be more specific than the variable, and that's
how polymorphism works.

Note the similarity to:
LinkedList list1 = new LinkedList();
List list2 = list1;
list1.addFirst("first"); // allowed, addFirst is a method on LinkedList
list2.addFirst("first"); // disallowed, addFirst is NOT on the List interface
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>

  #3  
Old May 30th, 2007, 01:45 AM
jupiter
Guest
 
Posts: n/a
Default Re: Generics references


"Mark Rafn" <dagon@dagon.netwrote in message
news:csvti4-qj1.ln1@hydra.dagon.net...
Quote:
jupiter <jupiter49byebyeSpam@msn.comwrote:
Quote:
>>I've noticed that the type in generic declarations is not
>>"inherited" by new references to the object. This seems
>>inconsistent to me but maybe there's a good reason for it.
>
Type inference is intentionally weaker than it could be. There
are good
reasons for it, I suspect, but it annoys me sometimes.
>
To wit:
Quote:
>>ArrayList<Stringlist1 = new ArrayList<String>();
>>ArrayList list2 = list;
>
List2 is explicitly NOT declared as a parameterized type. It
happens to have
an ArrayList<Stringin it now, but you could assign it an
ArrayList<Object>
later without error.
>
Quote:
>>list1.add(new Integer(3)); //compiler catches it as expected.
>>list2.add(new Integer(3)); //compiler is fat, dumb and happy.
>
Right, because you told it to be.
>
Quote:
>>I thought the idea of a reference was to point to the original
>>object and pick up all of the relevant data. Why not type?
>
Type is tricky. It's both a property of the variable and a
property of the
referent. The object type can be more specific than the
variable, and that's
how polymorphism works.
>
Great piece of insight, Mark. I never differentiated between type
and other properties, so this is nice piece to become aware of.

Quote:
Note the similarity to:
LinkedList list1 = new LinkedList();
List list2 = list1;
list1.addFirst("first"); // allowed, addFirst is a method on
LinkedList
list2.addFirst("first"); // disallowed, addFirst is NOT on the
List interface
Nice example indeed. This is yet another case of how implicit
casting can cause headaches for we normally-brained people.



 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,414 network members.