I guess we'll have to disagree. I'm not against generics entirely, just Java's implementation is screwy. I know it's for backward compatibility, but I really wonder....why bother.
i.e. allows the compiler to perform stricter static type cheking?
VERY VERY minimal. List<String> strings = new ArrayList<String>(); List unknown = strings; //it only takes one to break the chain List<Integer> integers = unknown;
i.e. is safer?
No, all the errors are in the exact same place. Using the code above, if I use Integer i = integers.get(0); I get the same ClassCastException as if I had: Integer i = (Integer) unknown.get(0);
i.e. is more (human) readable?
Well, using your example it's only more readable because the variable name wasn't descriptive. If you had rename "myFactory" to "stringList", they are both descriptive and readable. The nice thing is that the variable name is more readable everywhere, not just on the declaration. Also, in my example above, I think the cast is more readable, only because that's where the error will happen. With the generic list it makes me wonder "WTF happened? I thought this was a typesafe generic list that could only have integers!". Which of course is not the case at all. One old API will completely eliminate the type safety. And it will be years before all APIs give full respect to generic types. Don't get me wrong, iBATIS will support them. And as syntactic sugar they're nice in some ways. But nobody should be fooled. They are not safer. If you get a chance to attend NFJS, Venkat explains it best: http://www.nofluffjuststuff.com/speaker_topic_view.jsp?topicId=78 Cheers, Clinton On 3/13/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
"Clinton Begin" <[EMAIL PROTECTED]> scritti il 13/03/2007 14:42:04 > How is it safer? Let's assume to read the interface for the our factory (object loader) List myFactory( ... ); List<String> myFactory(...); Which of the two signatures provides better information? i.e. allows the compiler to perform stricter static type cheking? i.e. is safer? i.e. is more (human) readable? Of course internally (within the factory) we have to perform one cast. List<Strings> allows the client code use the list of looaded strings with its correct type without any (dangerous) casting. > In both cases the error occurs at runtime at the exact same point in > the code. And at runtime both are turned into a cast anyway. That's right. Exactly the minimum, i.e. one and only one casting, no more. A cast in necessary somewhere internally to the factory/loader. Externally is much better to use a generic container along with its actual type. I think that we agree. ciao
