Allow default methods to override Object's methods

Dan Smith daniel.smith at oracle.com
Fri Mar 1 12:12:27 PST 2013


The rationale is in the specification document, here:
http://cr.openjdk.java.net/~dlsmith/jsr335-0.6.1/H.html#H9.4.3

Note also that that the toString and equals/hashCode behavior of lambda-derived objects is implementation-dependent.  So even if a default 'toString' could be defined, the 'map' lambda would probably override it.

—Dan

On Feb 28, 2013, at 6:33 PM, Howard Lovatt <howard.lovatt at gmail.com> wrote:

> Playing round with the lambdas is generally going well for me, good fun.
> 
> However a couple of times I have wanted to override Object's methods, i.e.
> toString, equals, and hashCode. For example in a toy stream library I am
> playing with:
> 
> @FunctionalInterface public interface IntStream {
>  int next() throws Break, Continue;
> 
>  default void forEach(final IntConsumer consumer) {
>    try {
>      for (;;) {
>        try { consumer.accept(next()); }
>        catch (final Continue notUsed) {}
>      }
>    }
>    catch (final Break notUsed) {}
>  }
> 
>  default IntStream map(final IntUnaryOperator unary) { return () ->
> unary.applyAsInt(next()); }
> }
> 
> It would be great to override toString, equals, and hashCode just like a
> List does.
> 
> I think this has been discussed before I don't remember anyone having any
> examples of were it would be useful.
> 
> Do you think the above is a genuine example or an outlying case?
> 
>  -- Howard.
> 



More information about the lambda-dev mailing list