[type-annos-observers] Clarifying the receiver parameter

Markus Keller markus_keller at ch.ibm.com
Wed Apr 24 04:24:16 PDT 2013


The difference is that we're not talking about a 'this' expression here, 
but about the *declaration* of a method/constructor parameter.

For references to 'this', qualification is necessary in general to 
support references to all enclosing instances. Since the grammar already 
allows qualifiers, it makes sense to keep full generality and also allow 
redundant qualifiers there.

But there's no point in adding qualifiers for the declaration of the 
'this' parameter of an instance method. The reason is consistency with 
all other declarations. You also cannot qualify a method name, 
constructor name, field name, or type name in a declaration.

For constructors, the 308 spec decided to annotate the whole 
constructor, rather than allowing an explicit 'this' parameter like for 
instance methods. This works fine for top-level constructors, but it 
doesn't offer a way to annotate enclosing types of an inner class 
constructor. The syntactic kludge to allow annotations on the enclosing 
types is the superclass.'this' parameter on inner class constructors. A 
pure 'this' would declare the wrong element, so it needs to be 
qualified. And like the enclosing class' simple name is enough for a 
constructor name, the outer class' simple name is also enough to qualify 
the 'this' parameter of an inner class constructor.

An alternative to the current spec would be to drop section 2.1 point 4 
: "A type annotation is permitted in front of a constructor declaration, 
[..]". Instead of that, we could allow a 'this' receiver parameter for 
all constructors, where the 'this' would point to the constructed 
object. This would obviate the need for any qualification on 'this', but 
it has the drawback that TYPE_USE annotations on the whole constructor 
declaration could clash with annotations on the 'this' parameter's type. 
I think the rationale for the current design is to avoid this clash.

Markus



More information about the type-annotations-spec-observers mailing list