tag:blogger.com,1999:blog-3460761117713509776.post9180238172199974457..comments2016-03-10T12:56:30.576-08:00Comments on ?- true.: Re: Highbrow Java; Or: Java Generics and Why I Still Hate Themadimithttp://www.blogger.com/profile/04917371723395784281noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-3460761117713509776.post-36741948906238471492011-12-05T23:52:51.534-08:002011-12-05T23:52:51.534-08:00I agree with most paragraphs there are many traps ...I agree with most paragraphs there are many traps in generic implementation but still I think the advantages over old 1.4 code prevail.<br /><br />However I would add another thing into this list: it's impossible to define enum where each constant has different parameterized type:<br /><br />public enum Configuration<T> {<br /> DELAY<Integer>(),<br /> CONNECTION_STRING<String>(),<br /> FLAG<Boolean>();<br />}<br /><br />In equivalent 1.4 code type-safe enum pattern this is possible though more verbose and dropping all the 1.5 enum support.<br /><br />public class Configuration<T> {<br /> public static final DELAY = new Configuration<Integer>(Integer.class);<br /> ...<br />}<br /><br />Tomas ZaluskyAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-3460761117713509776.post-6164519089013436852011-11-28T10:15:21.161-08:002011-11-28T10:15:21.161-08:00Why do you claim that the following should be supp...Why do you claim that the following should be supported?<br /><br />public class C<A> {<br /> static A a;<br />}<br /><br />A is a property of the instantiated type - as such it doesn't make sense to refer to A from a static context. It's like accessing a non-static name from static-context. Reification or not - this should be disallowed.Maurizionoreply@blogger.comtag:blogger.com,1999:blog-3460761117713509776.post-18538432851028898432011-11-26T06:32:40.212-08:002011-11-26T06:32:40.212-08:00Did you have a look at Scala? It seems to solve mo...Did you have a look at Scala? It seems to solve most of the problems Java has while still being compatible with Java.Stevenoreply@blogger.comtag:blogger.com,1999:blog-3460761117713509776.post-26857547561746662692011-11-26T06:20:27.310-08:002011-11-26T06:20:27.310-08:00Well, Ada is OKWell, Ada is OKAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-3460761117713509776.post-14532627853065629552011-11-24T03:55:04.334-08:002011-11-24T03:55:04.334-08:00Yeah, that's pretty much my point: generics ar...Yeah, that's pretty much my point: generics are <i>good</i>, because every static type system <i>needs</i> some sort of polymorphism. I just can't stand the way they're implemented. It's all nice and shiny until you fall into one of the traps, whether it be arrays, downcasts, exceptions... and too often it results in <i>run time failures</i>.<br /><br />I've abandoned Java in favor of Haskell, and the nice effect of a <i>decent</i> static type system is that you can be sure your program will <i>run</i>. Except for running out of heap space/stack pointers/file handles, if a program compiles without warnings and I'm not using any unsafe function (which I can just grep my source code for) I can be very sure that my program <i>won't crash</i>. It's a ridiculously good feeling to write 500+ LOC with DB-interfaces and whatnot and your program runs bug-free on the first test. A good type system does that for you.<br /><br />You can explicitly cast types in Haskell. It's called 'unsafeCoerce' (talk about "don't use me") and I have never seen it used or even felt the need to use it. Whereas in Java, I can't write a reasonably non-trivial program without downcasting. That's where Java's type system is just too weak. I'll try to get LaTeX to run on this blog again, and then get into the details of why Generics are <i>weaker</i> than System F, but it might take me a while :-)adimithttps://www.blogger.com/profile/04917371723395784281noreply@blogger.comtag:blogger.com,1999:blog-3460761117713509776.post-53189098205877947552011-11-23T16:53:51.827-08:002011-11-23T16:53:51.827-08:00Thanks so much for this detailed answer!
1. Gener...Thanks so much for this detailed answer!<br /><br />1. Generics Have Poor Support for Arrays, agreed. As I stated in a previous discussion, I have long been following the "don't mix Arrays and Generics" policy (and been lucky enough not to work with libraries that would have forced me to abandon it). One could argue that Arrays are such a low-level language feature that you wouldn't want to work with them directly anyway.<br /><br />2. Inner Classes of Polymorphic Classes May Not Be Used as Array Component Types - I guess this is because types of non-static inner classes always carry the type parameters of their outer classes; they don't need to declare them. This is why this is legal:<br /><br />public class GenericClassWithInnerClass<T> { <br /> public class InnerClass { <br /> T o; <br /> }<br />}<br /><br />In any case, this is not an issue. You can just declare the class static to cut it off from the type parameters of its outer class.<br /><br />3. No Polymorphism for Exceptions – yuck. I didn't know about this one. This is getting ugly indeed.<br /><br />4. Static fields – another thing that doesn't mix with Generics at all. J. and I learned this the hard way in an early stage of Kahina development. Our response was quite parallel to the Arrays problem: move away from the use of static fields altogether. We haven't gotten rid of them completely yet and the process has been stalled for a while, but the direction is clear. In my opinion, refraining from using (non-final) static fields (as an example of parsimonious use of language features) lead to a better architecture overall. One could say that Generics aren't that bad, they just don't mix with some features that Java should never have had in the first place. But see immediately below.<br /><br />5. Downcasting Towards a Polymorphic Type is Always Unsafe – yeah, I think this really is the killer argument. Goes to show that Generics are, after all, an awful hack. Must be one of the reasons I really don't miss Java now that I'm mostly working with Python.<br /><br />I would of course be very interested what you have to say about the lack of expressive power, but I feel terrible for costing you so much time and energy... and convinced I am, after all.kehttps://www.blogger.com/profile/01767316422679015455noreply@blogger.com