default equals again, sorry

Paul Benedict pbenedict at apache.org
Thu Jan 23 13:34:06 PST 2014


Jose, the use of "static factory methods" in interfaces requires you to
provide a stock implementation.

interface Point(){
int x();
int y();
static Point of(int x, int y) {
   return new PointImpl() {
       public int x() {
             return x;
       }
       public int y() {
           return y;
       }
    };
}



On Thu, Jan 23, 2014 at 3:02 PM, Jose <jgetino at telefonica.net> wrote:

> Until now I've been able to overcome the limitation of default equals() in
> interfaces, by providing a defaultEquals() everywhere. Not very elegant but
> al least it works.
>
> But now I found an issue I don't know how to solve. I often use an idiom
> like this in the deepest loops of my code to restrict object allocation to
> a
> minimum. I call it stateless factory, but maybe there is already a more
> appropriate name for it.
>
>
> interface Point(){
> int x();
> int y();
> static Point of(int x, int y) {
>    return new Point() {
>        public int x() {
>              return x;
>        }
>        public int y() {
>            return y;
>        }
>     };
> }
>
> Until now it was working ok, but then I found I can't make equality tests
> with this construct.
>
> Any idea on how to overcome this limitation?.
>
>
>


-- 
Cheers,
Paul


More information about the lambda-dev mailing list