0

How this code works in groovysh:

whatever: { x = 1+1 }

I dont understand how Groovy execute this closure and how interpreted this:'\<anyText\> :'. For calling closure used () or call() according documentation. Groovy doesnt have ':' operator.

I seen this this code in Jenkinsfile and tried to analyze it. I used groovysh, code is worked, and i dont understand how it works.

1 Answers1

0

This is a part of a bigger Groovy's map literal, with String (whatever) as a key, and Closure { x = 1+1 } as a value. Highly likely, the brackets around the map literal [] are ommitted.

I can assume, that the code is evaluated in some DSL-builder, where the closure gets executed and together with the key is added in some way to the result.

x inside the Closure refers to a local variable either in DSL-script or in the builder.

If you look at a bigger picture, the things will start make sense very quick.

injecteer
  • 20,038
  • 4
  • 45
  • 89
  • I think the same, but i dont find this map or key in variables or whatever. I was enabled intepreter DEBUG mode, but also nothing. De facto it execute Closure without call it. Magic or standard Groovy behavior? – lionaft Mar 21 '23 at 11:11
  • no, in DSL-script the closure is declared, and later on it's called with either `closure.call()` or `closure()` – injecteer Mar 21 '23 at 11:14