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.