Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does case matter in gradle task method names?

In this question I was puzzled because I thought we could pass arguments to methods without parentheses. Indeed, you can pass arguments as a comma-separated list to a method like so:

task ListOfStrings(type: ExampleTask) {
    //TheList 'one', 'two', 'three' // doesn't work
    theList 'one', 'two', 'three'
}
public class ExampleTask extends DefaultTask {
    //public void TheList(Object... theStrings) {
    //    theStrings.each { println it }
    //}
    public void theList(Object... theStrings) {
        theStrings.each { println it }
    }
}

The code above works because the method name is camelCase. When using a method name that is TitleCase (commented out above) then gradle throws up an error:

  build file '/tmp/build.gradle': 16: unexpected token: one @ line 16, column 13.
         TheList 'one', 'two', 'three'
                 ^

SO, the question is, "why does the case of the method name matter?" Summarily, what is causing this behaviour? Is it a convention? And if so, where is it documented?

like image 1000
wbit Avatar asked Sep 19 '25 22:09

wbit


1 Answers

This is just the Groovy compiler treating any uppercase symbol as a class reference, rather than a method. There's an ambiguity here that you can fix by either:

  • using parenthesis Foo('one', 'two')

or

  • qualifying the method name this.Foo 'one', 'two'.

In general the convention is that classes are capitalized and methods aren't. Because Groovy is a dynamic language the compiler leans heavily on these conventions.

like image 118
Mark Vieira Avatar answered Sep 23 '25 06:09

Mark Vieira