Vritual extension methods
Deepak S Patwardhan
deepak.patwardhan at itaas.com
Wed Jul 4 07:37:00 PDT 2012
Hello,
The Defender methods v4 document (Brian Goetz, June 2011) has two
interesting foot notes (page 2)
5. It is a deliberate choice to not simply allow the programmer to include
the code of the default inline
within the interface. Among other reasons, it would violate the long-held
rule that "interfaces don't have
code".
6. The syntax of specifying the default implementation should match that of
specifying method references, if
method references are to be added to the language.
Consider the following code (which compiles successfully with build 39)
/* Complex.java */
/** Represents Complex numbers */
public interface Complex {
public Complex add(Complex b);
public Complex negate();
public Complex subtract(Complex b) default {
return this.add(b.negate());
}
public Complex multiply(Complex b);
public Complex invert();
public Complex divide(Complex b) default Complexes.divide;
}
/* Complexes.java */
public class Complexes {
public static Complex divide(Complex a, Complex b) {
return a.multiply(b.invert());
}
}
First Question:
As I am able to write a method body for subtract, the rule mentioned in
footnote 5, interfaces don't have code, seems to have been ignored. Has this
been finalized or will this be disallowed in a future build ?
Second Question:
Consider the default clause of the divide method. The syntax used for the
method reference is ClassName.methodName. In section 8 of State of Lambda v4
(Brian Goetz, December 2011), method references have syntax
ClassName::methodName. It also states that this syntax is provisional.
Example, map method of Iterable takes method references in this syntax.
List<String> names = .
nameLengths = names.map(String::length);
Will the syntax for specifying the default method change to (only) use
ClassName::methodName, in a future build ? Otherwise, footnote 6 also seems
to be ignored for now.
Regards,
Deepak S Patwardhan.
More information about the lambda-dev
mailing list