Allow default methods to override Object's methods

Howard Lovatt howard.lovatt at gmail.com
Fri Mar 1 13:31:11 PST 2013


@Dan,

Thanks for the reference. It is a pity that the scope of the lambdas isn't the same as an inner class and that they can't implement abstract class SAMs. For the examples I have been trying out the lambdas work well but are not object oriented and therefore don't fit with a lot of my existing code well. 

 -- Howard. 

Sent from my iPad

On 02/03/2013, at 7:12 AM, Dan Smith <daniel.smith at oracle.com> wrote:

> 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