- 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
Except that a List[ T implements Comparable ] cannot be inserted into. Oops. :)
That's why you have to tweak things a bit even with bounded wildcards.
Admin
So let's say we have a ArrayList[T implements Comparable] and you do:
List[String] slist = new ArrayList[String]
slist.Add(foo);
slist.Add(foo2);
Doesn't the compiler make sure foo is String? I feel like I'm missing something and you could enlighten me.
Admin
The advertising of type-safe collections is problely correct: if you use generic code, it will remain type safe. But calling it generic in the first place is problematic, because its not. Its currently just a poor-mans-polymorphy at the most. Type-variables are just half of the cake. The other half is being able to return a lambda function of which the type depends on the type or types of one or more of the given arguments. This actually required some sort of relational mapping. (parametric type classes in Haskell). So thats the full cake, right? Wrong. True generic programming would also mean that any more complex type is built out of simpeler types, so that we can write a type-safe structural equality function for any and every type we will ever create. This is currently possible through recursion, but its not type safe. Again: because the type systems of the main stream frameworks (.Net and Java in specific) are just too weak.
And for what?, is it really that hard to understand?
Maybe we should buy them a "What part of types don't you understand?" t-shirt that shows the Hindley/Milner typing rules? as offered here:
http://www.cafepress.com/skicalc.6225368
I had a good laugh with them.
Admin
Not only possible. Quite a number of programming languages actually do get it right: Haskell, Ml, Clean, Dylan ... and what Sun and MS are trying to sell as 'generics' is just a very very very small portion of what it really is. The funny thing is, implementing only a very very small portion like they did, really is more work. In Haskell type variables may appear at many different places:
Data BTree a = Branch (Tree a) (Tree a) | Leaf a
Here you define a generic binary tree. This is the sort of thing Sun and MS calls generic, although the rest of the world just calls it a polymorphic datatype. Now, for some real generics. In Haskell (GHC6 or higher) you can say:
Data BTree a = Branch (Tree a) (Tree a) | Leaf a deriving (Typeable, Data)
This automatically creates instances of the Class Typeable and Data for the BTree a type. You can now define ans use generic functions such as gmap. gmap takes any type and a function as arguments. It will apply the function to each bottom-node of the type. So imagine you have a BTree Int. You can say "gmap (+1) myTree" and it will update all nodes by one. The interesting thing is that gmap is defined _ONLY ONCE_. You can do the same thing with Java and such using reflection, but this is not type safe.
Admin
No. No it isn't more work, because Sun didn't want to recreate ML or Haskell. It wanted to add features to Java, the language it has already invested in and its customers have invested in. With that goal, it did rather well.
By the way, did you know that Simon Peyton-Jones now works for Microsoft Research, and helped develop some of the enhancements to C#?
Admin
He must have meant "to dispose of the colleague's legacy" and then backpedaled with a nicer word.
- Erwin
Admin
Yes but the error here is in way related to the use of generics...
you can get the same error with
Customer bob = new Customer();
Object o = bob;
o="some string";
Admin
Can't see anything strictly wrong there (other than re-using a variable (o) to mean multiple different things); you're just reassigning o; completely different scenario.
Marc
Admin
I'm sure his C++ code looks like this:
std::vector<void *=""> vector_of_stuff;</void>
That's the way, baby:
Come'n to the party:
THIS is owning the programming!!
THIS is a guru-style!!
lmao </size></void></void>
Admin
That would be because Java generics are just syntactic sugar. Generic classes are in fact just classes based on the Object class, with casting and boxing being performed as necessary.
In C#, however, generic classes are actually generated on the fly, as needed, based on the appropriate class, so no casting is necessary.
Admin
Blended lemons for breakfast?
WTF?
Admin
I'm a committed TDWTDF reader! (See article written 7 years from this one)
Captcha: luptatum - you say luptatum, I say laptutam