Preparing for the 0.2 draft

Mark Mahieu markmahieu at googlemail.com
Thu Feb 4 12:27:41 PST 2010


On 4 Feb 2010, at 05:56, Joshua Bloch wrote:

> Neal found 48 locations in OpenJDK 6 in which he claimed that evaluating
> "this" in the surrounding context would be helpful. I decided to take a
> closer look at them in hopes of learning something.

<snip>

> 4 could not take advantage of the
> "enclosing scope semantics for this," because the anonymous class has a
> method that shadows the one in the enclosing class (see (1) below for an
> example)

If "this" were evaluated in the context of the enclosing class, how could shadowing come into play here?  Wouldn't the former preclude the latter?

> (1) src/share/classes/com/sun/java/util/jar/pack/Histogram.java:187
> 
> class Histogram {
>    public double getBitLength(int value) {
>        double prob = (double) getFrequency(value) / getTotalWeight();
>        return - Math.log(prob) / log2;
>    }
> 
>    private final BitMetric bitMetric = new BitMetric() {
>        public double getBitLength(int value) {
>            return Histogram.this.getBitLength(value);
>        }
>    };
> }
> 
> The qualified this is necessary only because the anonymous class gives its
> sole method the same name as the one that it wants to call in its enclosing
> instance. But you can't name the method in a lambda expression, so having
> "this" evaluated in the surrounding context would not be necessary (or
> helpful) here.

The method is named anyway, by the BitMetric interface.  Surely it's whether or not that is visible in the body of an equivalent lambda which determines how helpful a given approach is?  For this particular example, the "evaluated in surrounding context" approach works out the same as the approach in the v0.1 draft spec, I think.  Or is that your point?


Regards,

Mark



More information about the lambda-dev mailing list