Java 5.0 goes the wrong way, Groovy goes too far
There are things that I think we could add to
Java to make it more attractive language to dynamic language developers and cut
the syntax down. At the same time, Groovy introduces many optional features
that I don't like from a maintainability and correctness
perspective.
I'm not going to repeat all the things I have
said in the past about generics, instead I am going to create a new language
with backwards compatibility to Java, better syntax, and more features. Here we
go:
1)
Auto-casting
To reduce redundancy in
the code I will introduce Auto-casting. What this does is that if an object
could be cast in place to satisfy a constraint then the compiler would do that.
It will fail if the auto cast is ambiguous or if it would fail if the programmer
put the cast in explicitly.
Map p = new
HashMap();
p.put("test",
"test");
String s =
p.get("test");
MyEJBHome home = new
InitialContext().lookup("java:comp/env/ejb/MyHome");
System.getProperty(p.get("test"));
2)
using a factory like new method instead of an
operator.
StringBuffer sb =
StringBuffer.new();
3) Reflective
invocation operator to allow objects to have shapes without explicit
interfaces.
Object s =
"test";
s =
s->trim();
4) Closure-like (lite)
anonymous inner classes. If an anonymous inner class created from an interface
has only one method, allow that method to be implemented without signature. Name
any arguments with the lowered classname plus a number if there is more than
one or allow them to name the parameters in the new
expression:
Runnable r = Runnable.new()
{ System.out.println("ran method");
}
FilenameFilter ff =
FilenameFilter.new() { return string.endsWith(".gif");
}
FilenameFilter ff =
FilenameFilter.new(File dir, String filename) { return
filename.endsWith(".gif"); }
Why don't
I just use Groovy? Well, I think that optional parenthesis, optional
semi-colons, optional line endings, really anything that makes the code writable
in a million different ways, probably doesn't help in the long run. I have
already run into many bugs in my own Groovy code that occurred because of the
optional syntax that is absolutely unneeded. What I really want is just a
slightly better Java, not a Java-ized Ruby
variant.
Anyone want to add any
features to this list? Its a fun exercise, if ultimately for
naught.
Posted: Mon - October 4, 2004 at 12:43 PM
|