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?
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:
Foo('one', 'two')
or
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With