The parameters of the Groovy Tip 32 method

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.