From virtual extension methods to mixins

Brian Goetz brian.goetz at oracle.com
Tue Jul 10 01:43:35 PDT 2012


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?  


On Jul 9, 2012, at 1:12 AM, François Sarradin wrote:

> Hi,
> 
> I would like to share a blog post. It explains how to get multiple
> inheritance of the state from the virtual extension methods.
> 
> "Java 8: Now You Have Mixins!" =>
> http://kerflyn.wordpress.com/2012/07/09/java-8-now-you-have-mixins/
> 
> François-
> 



More information about the lambda-dev mailing list