From virtual extension methods to mixins
Victor Nazarov
asviraspossible at gmail.com
Tue Jul 10 04:27:23 PDT 2012
On Tue, Jul 10, 2012 at 12:43 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
> I was going to leave this example alone, but I thought it was a good means to point out an interesting Puzzler.
>
> Let's assume that we fix the two most obvious problems in Francois' example -- thread-safety and memory leaks. It turns out that this idiom is still just so fundamentally broken as to be unrescuable.
>
> // Don't code like this!
> interface FakeBrokenMixin {
> static Map<FakeBrokenMixin, String> backingMap = Collections.synchronizedMap(new WeakHashMap<>());
>
> String getName() default { return backingMap.get(this); }
> String setName(String name) default { backingMap.set(this, name); }
> }
>
> interface X extends Runnable, FakeBrokenMixin { }
>
> X makeX() { return () -> { System.out.println("X"); } };
>
> X x1 = makeX();
> X x2 = makeX();
> x1.setName("x1");
> x2.setName("x2");
>
> System.out.println(x1);
> System.out.println(x2);
>
> What can this program print?
>
The problem remains even without lambdas. Any abstract factory that
can reuse instances will cause the same problem.
--
Victor Nazarov
More information about the lambda-dev
mailing list