Skip to main content

First Class Java: Call of Duty 4

Posted by evanx on March 25, 2008 at 4:24 AM PDT

So i had this vague idea some weeks ago for some blog articles to keep myself amused if no one else. The idea is... to dream of a Java Refined. Not something totally different like JavaFX Script, or Scala et al, but some kind of evolution of the Java language that would be dreamy with a dollop of double-thick cream on top...

So i started making a list of things i would want, which went something like: first class properties; first class meta stuff like methods (FCM), fields and what-not; better annotations; better generics; structured literal data (eg. XML) and queries and what-not in the language; first-class handling of externalised data like localisable text and customisable what-nots.

rustic-school2-crop.jpg
What is the point of this exercise you ask? To have something to think about and doodle about when i'm in my favourite coffee shop, and a topic to blog and converse about like now.
Now i can't remember all of my wishlist exactly because i left it at home and i'm quite literally a thousand miles from home at the moment...

Anyways, i remember that when i looked at my list, i thought, what i want is "first class" this that and the other. So i thought lemme call this exercise "First Class Java."

But enough about me, what about you - how would you evolve the Java language? I'll collate all ya all's ideas, mind-meld them into some kinda concensus of me, myself and you, and write them up in some ongoing fashion for further consideration and discussion, just for the hell of it...

Having said that, if i win the lottery, i'll sponsor a team to create a slightly new, very Java'esque language with Netbeans support et al. To be honest, first i would buy myself a few first-class houses, sports cars, jets, yachts, etcetera, then after i've bought all that, i'll sponsor some first-class schools and clinics and what-not in my country (hey i also wanna help Obama make the world a better place), and then after that, i'll sponsor First Class Java with the petty change. Heh heh.

Comments

Right. I think people (including myself) are lazy. I also think exceptions shouldn't be caught. Except by default at the top of the control loop and also in specific circumstances when you know why you want to catch an exception. If you know when and why, you'll take care of it correctly. If you don't, you'll corrupt data. Finally, on the other hand, is vital everywhere, and that's one reason why closures (or ARM blocks) are so important.

@tompalmer - Given your assumption every line of code would need to be wrapped in a try catch block. Why then would you want to remove checked exceptions? The try catch block is going to be there anyways. The only reason to remove checked exceptions is to be lazy and not care if something goes wrong. Anything that can be caught by the compiler is a bug that will never go through the test/report/fix/submit process.

@aberrant/Collin - The way I look at it is, "Life is exceptions, and anyone who says differently is selling something." (Well, maybe not quite but movie references are worth using sometimes anyway.) Or rather, Java requires you to always assume that an exception could be thrown at any time. And I don't there's any realistic way to get around that. I've never seen anyone do better in any other language either.

Also, if you have easy multi-line strings, replace JavaDocs with annotations. No need for redundancy.

No need for named parameters if you have good object literals, like Groovy. Groovy operator overloading is also the best idea I've seen along those lines.

Also, get rid of public fields (at least without extra pain). Appearing to declare a public field should really just be generating getters/setters - and a private field if not abstract (or in an interface). Both Groovy and Scala do this nicely.

Another last thought. I'm not a big Groovy user, but a lot of their thoughts have been "how can I make this better than Java while still looking like Java". Backporting much (obviously not all) of that into "better Java" would not necessarily be a bad idea.

On the specific issue of "improved generics", which I very strongly support, I particularly like this piece by Howard Lowatt on artima.com. It argues for simplifying generics by removing wildcards, and making generic parameters co-variant by default like arrays are. I really like that. We've got to put an end to what's termed "generics hell" painful experience in Java that's mainly caused by wildcards.

@tompalmer

You are right, exceptions is a polarizing topic. I have the opposite opinion. I believe RuntimeException should go away, but I'd never push for that. I've just had to deal with one too many 3rd part libraries that decide to throw RuntimeExceptions instead of actually telling me something could go wrong. Then errors happen in the field that can't possibly have been planned for. I do agree that some of the pain could could be removed. The is why I want A reusable exception handling system.. It would allow one to create standard ways of dealing with checked exceptions. If you choose to re-throw them all as RuntimeExceptions great, have fun. I want to be able to handle a specific type of error condition once and never have to write the code again.

Collin

thanks for the comments - please keep them coming! and please point me towards any literature/proposals i might incorporate in this exercise eg. to blog about further.

I doubt i will implement anything, not even a code convertor/generator (from an "enhanced java" to regular java), or Netbeans module providing some convenient views, and certainly not compiler changes, cos i don't have the time or skills.

What i would enjoy doing is dreaming about and writing about an impossible ideal i.t.o. beloved Java :)

Oh, and a primary goal needs to be that "better Java" should be simpler than Java. Fewer rules for beginners while staying better for experts (again without more rules).

Scolebourne is right on the money in many cases. I'd also add that the checked exception model needs reworked or removed. I've learned several languages from before and after Java, but I've never seen another with checked exceptions. And it's not for lack of people being exposed to Java. I know it's a polarizing topic, but any "better Java" needs to make it easier to deal with errors correctly. And I'm absolutely convinced that even just making checked exceptions go away would do that.

Side note on the "==", that should always do a fancy "equals()" call, for all classes, just like Groovy. It's just that implementing "equals()" for non-value classes is an anti-pattern.

Reified generics would be great, but sadly incompatible until the JVM supports at least the runtime generics info for all generic classes/functions. You can't be better "Java" and have ArrayList work differently from classes defined in the new language (where type info could technically be secretly stored). Failing JVM support, all generics really should be just considered that covariant casting is fine. Make wildcards an endangered species.

The FCM vs. BGGA thing is vital since to "look like Java" might require looking like whatever syntax wins for Java itself in this arena. Unless the popular forking happens now.

For "better Java" to win, it also might need Sun's blessing before too long. And it definitely needs strong IDE support.

Finally, I'd recommend that Kijaro would be a great place to start the "better Java" (just watching for trademarks, of course). Just impose the incompatible rules based on a compiler switch or perhaps a different file extension.

@scolebourne Thanks, that makes a lot of sense.

By value classes I mean simple final, immutable classes. In particular == could be defined to mean .equals() for value classes. In a way they have some of the functionality of enums, but where the developer has to still write some code. Example uses are date and time, but also Integer/Long/Double and Complex. Basically the aim is to make writing simple immutable 'value objects' easier.

I also vote for properties and method/field/constructor references. I've messed around with FCM and found it very productive. From that experience I feel that properties will also be very useful.
  • I'd like a multi-line string literal.
  • A string literal that does not support / as an escape char so that I can write regular expression without an unbelievable amount of /'s. Like the C# @ syntax.
  • JDK support for a system like Foxtrot that provides a synchronous way of interacting with the event dispatch thread.
  • FCM style closures
  • A reusable exception handling system.
  • Some type of delegate system that lets me directly expose a method from a contained object instead of having to wrap it in a call with the exact same parameters.
  • Date and Time literals for Day, Month, Year and supported math operations.
  • Named Parameters
  • Simple Generic data models that use properties or method references and collections to back Swing components.
  • Constructors that can grab their Generics info from the variable they are being assigned to. Ex: List<String> list = new ArrayList();
Give me more time and I'm sure I could come up with more.
@scolebourne - I'm unfamiliar with the term "value classes" could you explain a little?

Collin

The Pizza Language (http://pizzacompiler.sourceforge.net/ ) implemented Generics, function pointers, case classes and pattern matching. The Generics idea was imported (almost with the same syntax) by Java 5.0; why don't import the other ideas as well?

- more support for immutable objects and pure functions. A little marker on each method that mutates state would be nice, too. - pattern matching, something like: match(n : inputStream.read()) { n == -1: blabla n>=0: blabla } plus a way to mark the return value(s) of a method so that you are forced to pattern-match it.

I think that there is definitely a desire in the wider community for a 'better Java' - something that fixes the flaws in Java but is 90%+ compatible. First Class Java isn't a bad name either :-)

It will include all the favourites like properties, method/field/constructor references, value classes, better string handling, more literals (but not XML). It would also handle nulls properly (non-null by default). The key however is for it to instantly be readable by a Java developer - existing developers must be able to make an accurate educated guess at each piece of the new syntax.

Rather, I meant "finally" (the keyword) is vital everywhere.