- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Perl hashes, brought to managed code.
Admin
So what's worse...generic databases, or generic types?
Be specific. ;)
Admin
But... by... being... generic ...... it's not tied to any code!!!
WTF???!?!
Admin
Fucking ar-tard!
3rd post?
Admin
Managed code has had Perl hashes since day one. :)
Admin
Admin
Generics sound really really neat. So many times, I've written custom classes that were boilerplate lists or maps restricted to a specific type, and thought if only we had generics a lot of cookie-cutter code would disappear. I'm sure generics will be wonderful. But I really do fear an entirely new species of WTF<Exception>
Admin
I think its safe to file this one away under the "Just doesn't get it" category.
Admin
Admin
i love it when they go against the grain .. just I hope that the tree lands on this programmer and rids the world of his unmanageable code!!!
Admin
<Dr. Evil>Scott, you just don't get it, do you?</Dr. Evil>
Admin
So given that C++ has no basic Object class...
what was this guy doing in C++?
Admin
Void pointers, baby! Woohoo!
Admin
My brane just exploded.
Admin
Meh bane jut splode
Admin
Generics may sound great but really they are a dirty hack that does not actually provide any type saftey at all.
List<Integer> myList = new ArrayList<Integer>();
List list2 = myList;
list2.add(new String());
Code compiles, now iterate through your genericized myList and see what happens.
Admin
Void pointers are wonderful for things like that, especially when they are not used. How do people like that get any kind of guru status? I would guess money, but who pays them?
Admin
I write code that looks like that all the time, and it's not entirely stupid (although how he was doing in C++...who knows). Depending on the warning level the Java compiler is set to, it will bitch at you for declaring
Dictionary = new Dictionary();
instead ofDictionary<Object, Object> = new Dictionary<Object, Object>();
Eclipse will, too, and the Sun people recommend you do it, too. Of course, that doesn't mean that's why this guy was doing it...Admin
If this really does as you say, then it would, in my opinion, be a defect in the implementation of generic programming in C#. C++ would not accept such nonsense. You do not want a supposedly modern, managed language to be less safe than a volcano language like C++. Why aren't the template/generic/type parameters for list2 inferred from the type definition of myList? It should be blindingly obvious to the compiler! And why does C# allow a generic class to be instantiated without proper template/generic/type parameters? Does the compiler at least emit a warning? What's the point of a static language if the compiler doesn't perform bondage-style static checks? Why not just use a fully dynamic language then?
</integer></integer>
Admin
I believe he's referring to Java, not C#. In C#, there is no non-generic form of the List class. So line #2 above causes a compilation error.
-ds
Admin
DUH!!!!
Admin
That's not exactly true, at least with Java 1.5. Depending on the compiler options you give it (or annotations) you can ignore the compilation errors. In Eclipse however, I have to specify I want the compiler to ignore these errors otherwise it will have issues. This is true since java has it's own issues with casting collections.
List<Integer> newList = (List<Integer) oldList
You see this a lot when working with Hibernate.
-Zach
Admin
You're right, it's Java.
Admin
Do I meant to quote this in my previous post.
-Zach
Admin
Well I just keep making mistakes I don't know how to display tags in here but I'm meaning something like
List {Integer} newList = ( List {Integer}) oldList
please replace { } with greater than, less than signs.
-Zach
</integer)></integer>
Admin
There, Fixed that for you.
</integer></integer>
Admin
The real WTF here is that Java lets you ignore compilation errors. I can see it now:
<grommet> How do I make my code compile? I'm getting this error...
<guru> Ignore the error.
<grommet> AWESOME, thanks! Now why doesn't it work?
Worse... I can see that happening in a business setting.
Admin
That's true. That's why I sort of butt heads against Annotations in some respects because to me their are glorified pre-processor directives. However seeing some things with frameworks like Stripes [Stripes] They have some uses.
Admin
That's not to say that C# doesn't have its own issues with generics...
List<Int32> myList = new List<Int32>();
IList myList2 = myList;
myList2.Add("Hello World!");
That compiles, but will at least throw an exception when line 3 is hit.
-ds
Admin
They probably needed it as a feature to win over the On Error Resume Next-crowd from Visual Basic. Or perhaps the Java spec writers had heard of this site and wanted to ensure amusing code for the next many years.
Admin
Java generics are actually much older than Microsoft's. IIRC there existed independant prototype implementation for long time and than there was JSR14 prototype compiler from Sun Early Access available something like year before 1.5 final.
Something didn't quite work out ...
- CAPTCHA Validation Incorrect
Admin
It doesn't work with lists (at least when I tried it), but List<t> implements both IList<t> and IList interfaces. So you could do:
and it will compile fine and dandy (it will throw an exception at runtime on line 3).
Admin
I might be wrong here but I'm pretty sure Java doesn't let you ignore errors. It lets you ignore warnings, which is what the java compiler gives you if you don't properly (in its view) genericize things.
Admin
Reminds me of about 10 years ago, when I was programming in Delphi. We joked that we could solve all of our bugs by just having the code look like (I forget exact syntax)
Try {
Application.Run()
}
catch(...) {
}
Brillant!
Admin
Java does NOT let you ignore compilation errors!
The case here is that casting a parametrized type to a raw type merely issues a warning, not an error. The reason is backwards compatibility. If you couldn't cast a List<Integer> to a List, how would you pass it to a pre-Java 5 library that uses the raw type?
But of course, if you ignore warnings, it's your own bloody fault. Never ignore warnings! When they occur, check the code carefully, make sure it's really what you want. Then add a SuppressWarning annotation to mark the fact that you've double-checked.
http://blogs.msdn.com/michkap/archive/2006/01/30/519993.aspx
Admin
That's less of a generics issue and more of an inappropriate use of IList issue.
Plus, I think you'll get a warning. Need to test it, though.
Admin
Actually, now that I look at it again, that shouldn't compile because you forgot to cast myList to IList. Looks like I need to get an account. :)
Admin
"Brilliant", said Paula Bean.
Admin
That doesn't compile. IList is treated as a generic and requires a type declaration.
Using the generic type 'System.Collections.Generic.IList<T>' requires '1' type arguments
Even if you have a code sample of that form that does compile, I'd vote the problem is with the developer, not the language. List<> has to implement the IList interface in order to be usable; using the interface to access the object and intentionally trying to break it seems silly.
</int32></int32>
Admin
</int32>Bah.
Ok, it compiles and does throw an exception. You just have to include System.Collections as well as System.Collections.Generic.
I still vote it's misuse of an interface. :)
</int32>
Admin
You're right, it's not a compiler error, but a warning. In Eclipse I modded up those particular warnings to be error in code. Similar to x = x or Unused Imports, and added the @SuppressWarnings("unchecked") for bringing things out of Hibernate.
-Zach
</integer>
Admin
Maybe he created a class Object, and derivied all other classes from that one? its a good way to ensure generic and polymorphic data structures in c++
Admin
I bet someone who was kicked out of C++ development due to gross misuse of C++'s template features will find a way to... dare I even suggest... replicate C++'s... expressivity... that both MS and Sun vehemently denied that generics would have.
Today's post was pathetic in that regard, it was almost readable. Hope someone tries harder. Come on! Something worse? You know, remove-a-space-from-a-funny-location-and-it-won't-compile worse?
</exception>
Admin
Use or misuse, it means the advertising hype of "type-safe collections" is a myth. It means that "provably safe casting" is right up there with believing in the tooth-fairy.
The entire point of Generics is to trap these casting errors at compile time rather than runtime. If all we do is reclassify these runtime errors as misuses we haven't really accomplished anything.
Making the generic lists implement the old list interfaces for backwards compatibility is just daft. Reminds me of the "refused bequest" bad smell in code. Except now the smell is ingrained in the core language of both Java and .Net
My ILists, the Generics, they do nothing!
Admin
> if only we had generics a lot of cookie-cutter code would disappear.
Check out Ruby or Python. It will asplode your brane.
Admin
You're right that it doesn't compile, but does throw an exception.
I don't know why you would do this in a 'production' piece of code, but I guess that is what this website is all about.
long time reader, first time poster :)
Admin
That code does compile, sure, but as others have pointed out - it does give you a warning. If you ignore the compiler warning then of course it's your own responsibility to be type-safe!
Admin
yeah! What he said!
Admin
Assuming you are referring to Java, you are right on one point - generics are a hack. The point where you are wrong is the assertion that they do not provide type-safety in your provided case. The Java generics implementation makes the following guarantee: "Your parameterized types will not fail at runtime provided you receive no compile-time warning" - your case meets this contract, since it will generate a compile-time warning - in which case, you have left the context of the generics contract.
On the issue of the flaws of generics, I cannot reveal everything, since after all, I recently left my job implement the spec., working with JSR groups, etc, and I will upset The Filth that I was up until recently employed by, but I can take a type such as java.util.ArrayList (as you have provided) and ask you to try implementing it yourself without receiving a compile-time warning - good luck with it :) When you fail, ask yourself "why?" - then you will discover one of many "generics hacks".
</integer></integer>
Admin
As others have said, if you're ignoring warnings from the Java compiler, you deserve what you get.
Of course, if you have no control over who's going to be messing with your list, this will force it to stay "pure":
List<Integer> myList = new ArrayList<Integer>();
myList = Collections.checkedList(myList, Integer.class);
// Go ahead and ignore compiler warnings, but it will throw an exception when you try to "pollute" it at runtime
You did read the "New Features and Enhancements" section of the JDK's documentation, right? You did notice the new methods mentioned in http://java.sun.com/j2se/1.5.0/docs/guide/collections/changes5.html , right?
</integer></integer>