Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle 4.times. What is $it?

Tags:

java

gradle

I'm just newbie in Gradle and while I was reading the Gradle manual I came across the follwoing code example:

task count << {
    4.times { print "$it " }
}

It prints 1 2 3 4. Why? What is $it and why if we replaced $it to "a" we would receive

a a a a

Moreover

task count << {
    print "$it "
    print "$it "
    print "$it "
    print "$it "
}

gives the output task ':count' task ':count' task ':count' task ':count'

like image 668
St.Antario Avatar asked Oct 23 '25 01:10

St.Antario


1 Answers

From http://groovy-lang.org/closures.html#implicit-it

  • a closure defines an implicit parameter - 'it'

From http://grails.asia/groovy-times-loop-examples

  • 'times' is a method for looping
  • You can refer to the variable it to know the current index of the loop.
like image 172
Avinav Avatar answered Oct 25 '25 14:10

Avinav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!