Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does not capitalizing the class name cause a compiler error here?

Tags:

groovy

This Groovy script runs fine:

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

This one fails with a compilation error ("unexpected token: public at line: 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 the convention is for class names to be capitalized, but I thought it was just a convention. What exactly causes the compile error?

like image 303
seansand Avatar asked Jan 23 '15 19:01

seansand


People also ask

Why do programmers capitalize the second word?

Pascal case naming convention The use of a single uppercase letter for each additional word makes it easier to read code and discern the purpose of variables. The term Pascal case was popularized by the Pascal programming language. Pascal itself is case insensitive, so the use of PascalCase was not a requirement.

How do you capitalize letters in Java?

The simplest way to capitalize the first letter of a string in Java is by using the String. substring() method: String str = "hello world!"; // capitalize first letter String output = str. substring(0, 1).

How do you capitalize a character in a string in Java?

In order to pick the first letter, we have to pass two parameters (0, 1) in the substring() method that denotes the first letter of the string and for capitalizing the first letter, we have invoked the toUpperCase() method. For the rest of the string, we again called the substring() method and pass 1 as a parameter.

How do you capitalize the first letter of every word in Java?

We can capitalize each word of a string by the help of split() and substring() methods. By the help of split("\\s") method, we can get all words in an array. To get the first character, we can use substring() or charAt() method.


1 Answers

According to a Groovy mailing list thread from 2008, where a similar question was posed, Paul King explained:

Yes, the grammar currently looks for uppercase types only in declarations (apart from the primitive types).

In a more recent, unresolved Groovy JIRA ticket regarding lowercase class names, blackdrag comments that:

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

Barring a deeper exploration of the tokenizer, I'll just chalk this up as another minor inconsistency between Java and Groovy due to Groovy's syntax flexibility. And instead of thoroughly implementing a way to tell if a token is a type or method name in this context, Groovy takes a short cut and only assumes it can be a type name if the token matches a primitive or begins with a capital letter, as conventional Java types would.

like image 67
bdkosher Avatar answered Oct 09 '22 03:10

bdkosher