Why doesn't caching a class name cause a compiler error here?

This Groovy script works fine:

println 0; class MyClass { public MyClass(int j) {}; public MyClass method() {return this}; } 

This is a compilation error ("unexpected token: public on row: 5, column: 4")

 println 0; class MyClass { public MyClass(int j) {}; public MyClass method() {return this}; } 

The only difference is the capitalization of the class name. I know that the convention is for class names to be capitalized, but I thought it was just a convention. What exactly causes a compilation error?

+5
source share
1 answer

According to Groovy, a thread on the mailing list since 2008, where a similar question was asked, Paul King explained:

Yes, grammar currently only searches for uppercase types in declarations (except for primitive types).

In a later, unresolved Groovy JIRA ticket regarding lowercase names, blackdrag comments that:

The problem is that in Groovy variable names (unlike Java), method names and class names can share the context, making it ambiguous.

By prohibiting a deeper study of the tokenizer, I will simply do it as another minor inconsistency between Java and Groovy, thanks to the flexibility of Groovy syntax. And instead of fully realizing the way to determine if a token is a type or a method name in this context, Groovy makes a short call and only assumes that it can be a type name if the token matches the primitive or capitalized, since ordinary Java types will be.

+7
source

Source: https://habr.com/ru/post/1211785/


All Articles