The implementation of default methods (original) (raw)

Brian Goetz brian.goetz at oracle.com
Fri Dec 14 08:30:29 PST 2012


There are several layers of this issue.

  1. How to document implementation properties of the default in a way that does not commit to doing things this way forever. This is analogous to "This implementation currently..." notes that are sometimes seen in class method documentation, and I don't think requires much more than that. For example, if the default implementation uses a bad algorithm, it could document this as "this implementation currently uses an O(n^2) algorithm; subclasses that care about performance should probably provide a better implementation." This does not commit us to maintaining a bad algorithm forever, but does provide useful detail to users and subclass maintainers.

  2. How to document what a subclass can count on, so they can make a decision as to whether they need to override the method or not. Here, Doug's examples with putIfAbsent and friends are good; we document "this implementation behaves as if...", which is a kind of specification for the default, and the default had better continue to behave that way.

  3. Retrofitted defaults in already-specified classes. This is the Iterator.remove issue being discussed on corelibs, where the issue is whether adding a default for remove that throws UOE now becomes part of the specification for Iterator.remove, and whether all compliant implementations much have a similarly-behavior Iterator.remove. This one is trickier than (2) because it may be changing the specification of existing code. So the question here is, what does it mean to add a default on Iterator.remove? Does that make undue work for JDK vendors who don't use our java.util classes? How do we write the spec to make it clear that having a default that throws UOE is part of the spec now?

Here's my proposed answers:

  1. Document "Implementation note: The default implementation currently..."

  2. Document "The default implementation behaves as if..." (Or whatever Doug's proposed wording is.)

  3. Document "The default implementation MUST"

On 12/14/2012 8:06 AM, David Holmes wrote:

Okay we now have several threads across the public lists debating the issue of whether the implementations of default methods that we are adding to the core libraries are the required implementation for those default methods or simply an implementation.

Can we get a definitive position on this? Thanks, David



More information about the lambda-libs-spec-experts mailing list