Atom Feed SITE FEED   ADD TO GOOGLE READER

Properties would work nicely with First-class methods

First-class methods (FCM) is a compelling proposal to enhance the Java language so that methods, fields and constructors can have full type-safety.

In today's Java, we write this:
Method getter = Action.class.getDeclaredMethod("isEnabled", new Class[] { });
Method setter = Action.class.getDeclaredMethod("setEnabled", new Class[] { boolean.class });
boolean fooEnabled = getter.invoke(fooAction, new Object[] { });
setter.invoke(barAction, new Object[] { false });
This code is unnecessarily verbose. The caller needs to catch a NoSuchMethodException because the String method names are not compile-time checked.

FCM fixes these problems! The above code becomes this:
Method getter = Action#isEnabled()
Method setter = Action#setEnabled(boolean)
boolean fooEnabled = getter.invoke(fooAction, new Object[] { });
setter.invoke(barAction, new Object[] { false });
The method instances themselves can even be expressed safely, which adds even more type-safety:
#(boolean(Action)) getter = Action#isEnabled();
#(void(Action,boolean)) setter = Action#setEnabled(boolean);
boolean fooEnabled = getter.invoke(fooAction);
setter.invoke(barAction, false);
Naturally this is very powerful! The syntax is unfamiliar but adjusted to generics and we could adjust to FCM.

Applications to Properties
FCM makes a very concise and typesafe syntax for Properties possible - just specify the getters and setters:
Property<Action,Boolean> property = new MethodsProperty(
Action#isEnabled(), Action#setEnabled(boolean))
boolean fooEnabled = property.get(fooAction);
property.set(barAction, false);
By implementing JSR-295 beans binding with a Property interface, we're making it possible for the project to leverage future Java language enhancements.
Thanks for the writeup. I'd just add that if we get a property keyword in Java, then we could use the syntax Property[Action,Boolean] property = Action#enabled, which is even better ;-)
This is another example how to destroy a simple yet powerful language. Despite being overly long the current version is simply understandable by any Java author. The new version introduces (another) new syntax for a problem that simply is non-existant. Java needs to be careful not to go the (bad) way C++ goes being so complex that one can write code that is completely not understandable for others.
Not to mention stupid decisions like auto-boxing which is so badly designed that I wish someone would extend the javac to optionally throw up an error for any case of auto-boxing.