From virtual extension methods to mixins

Brian Goetz brian.goetz at
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();


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!" =>
> François-

More information about the lambda-dev mailing list