What is the meaning of this?

Alex Blewitt alex.blewitt at gmail.com
Fri Jan 29 01:34:42 PST 2010


On 29 Jan 2010, at 06:56, "Zdenek Tronicek" <tronicek at fit.cvut.cz>  
wrote:

> You view closures as a concise notation of anonymous
> classes and then you, naturally, expect that 'this' refers to the  
> instance
> of such class. "Transparency guys" view closures as blocks of code or
> functions. Then, 'this' should refer to the enclosing instance.

I believe this is a good point. If lambdas were e.g. #DEFINEs in  
another language, then I can understand the "transparency" argument.   
However, I suspect that more Java developers will be familiar with  
(anonymous) inner classes. I have no data to back this up, but a  
comparison of widely used dynamic languages (Python, Ruby, Groovy,  
Scala) might be a good model to follow. Sadly there aren't widespread  
experience of languages that have a transparent form (e.g. non-local  
returns) outside of C macro functions - and those support function  
pointers too.
>

Maybe there's an argument for introducing a different form later, but  
the whole class of SAM examples is based on the understanding that  
they are inner class factories.

Lastly, observe that if 'this' is permitted to refer to a lambdas  
anonymous class then it is still possible to refer to the "outer" this  
by using qualified this expressions. However the reverse does not hold.

Coming back to the visitor example, "this" referring to the lambda is  
of absolute importance when recursiveky descent parsing:

(#void(Component c) { if (c instanceof Container) for (Component cc :  
c.getChildren()) this.(c) else System.out.println(c.getName()). 
(component)

Alex 
   


More information about the lambda-dev mailing list