Allow default methods to override Object's methods

Howard Lovatt howard.lovatt at gmail.com
Thu Feb 28 17:33:47 PST 2013


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