Although I'm not a fan of messing up the Java language, I had another idea, I'd like to share:
An
extended type-cast for single-method interfaces, which succeeds if the signatures are compitable. More precise, the target's method could be overridden by the one of the source interface, the syntax could be:
(A as B)(Note: contravariant arguments would be also possible)
I guess it's is highly related to the ongoing debate if and how to integrate closures (
CICE,
FCM and
BGGA). Although, I think the last one has its pros and I really like the idea of first-class methods (place my vote here), the remaining one really has a mighty ally:
simplicity.
However, the absense of method/functions type, which can be assigned only depending on their signature types, makes it look a bit clumsy. With the proposed extended type-cast, things would be more balanced as this
example shows.
Just, for those who didn't have noticed yet, the main thing is
(Bar as Foo), but it looks nicer within the other syntax sugar as well:
- calling the single method of an interface, its name can be omitted
- simplifyedcreation of anonymous classes, which implement single-method interfaces
- usage of
var/final instead of specifying the reference type
EDIT: Modified the source to match the
BGGA proposal accoringly to Neal Gafter's comment.
Labels: Java, RFE