The groovy language does a lot of good work on the input parameters of the method, some of which can improve the readability of the code, and some can provide easy and agile coding. In the previous text, we have introduced some, such as the Groovy Discovery map and DSL. In this series, we will introduce some of them that have not been mentioned before.
First, it is about the use of parentheses when invoking a method.
In the code of the Groovy language, we may already be proficient in using the following statement:
println 'hello,world'
As we all know, the above statement is equivalent to the following statement:
println('hello,world')
That is to say, in the groovy language, we can omit the pair of parentheses that invoke the method, the first of which is to reduce the keystroke of our input brackets, improve the speed of coding, and the second is to improve the reading of the code, which is one aspect of DSL. For example, the following statement:
def word = 'Are you ok?'
println word
is better than "println (word)" reading.
However, it is noteworthy that the parentheses in the method call are not arbitrarily omitted. Sometimes, omitting causes compilation errors. Like the following line-wrapping statement:
println()
You can't omit the parentheses and write the following:
println
In addition, the following statements are not compiled correctly:
def list = [1,2,3,4]
def list2 = list.subList 0,1
But the following statements can be compiled:
def list1 = list.subList(0,1)
list.subList 0,1
In short, when you find that there is a compile error when you omit the parentheses in the method call, you should add the parentheses.