Community
Participate
Working Groups
A somewhat frequent source of bugs in Java is the result of a copy/paste that includes the current class name. For example, consider the ubiquitous Java: static final private log = LoggerFactory.getLogger(com.package.Clazz.class) Obviously, "this.getClass()" can't be used in this static context. Might it make sense to make "typeof()" - that is, typeof with an empty argument - mean the current class? Or otherwise provide access to this type without explicitly naming it?
I understand your issue, and on first sight liked the basic idea. So lets assume you want to express something like: static Logger LOG = Logger.getLogger(typeof(MyClass)) -> static Logger LOG = Logger.getLogger(typeof()) The typeof operator belongs to the XTypeLiteral in Xbase.xtext: XTypeLiteral returns XExpression : {XTypeLiteral} 'typeof' '(' type=[types::JvmType|QualifiedName] (arrayDimensions+=ArrayBrackets)* ')' ; The proposal above would require to make the block in paranthesis optional, thus type could be null. When the type attribute can be null this might have many implications on the existing code. The referred JvmType would have to evaluate to the inferred JvmType where the XTypeLiteral is defined in. Currently I'm not sure if this can be evaluated at the time it is required, since the inferrer might resolve the type at a later stage. I think this has to be evaluated if that is possible. However, this approach won't work with the other way to express type literals. It is valid to leave out the typeof operator and just name the type literal: static Logger LOG = Logger.getLogger(MyClass) There is no way to abbreviate this, and nowadays most time type literals are used without the operator. This would force people to use the typeof operator artificially. I am undecided, but tend to reject the feature.