TIL: Closures in Groovy

I interact with Groovy solely because Jenkins uses it to define pipelines.

Pros:

  • It’s in the Java ecosystem
  • It’s a “real” language (turing complete unlike YAML)
  • It’s not YAML

Cons:

  • No static typing
  • Linters/formatters aren’t great
  • Jenkins is quite hard to work with and has surprisingly poor tooling

While updating some Groovy scripts, I wanted to update a utiltiy method to take a closure in a nicer way. The method looked like this:

def hello(clo, first = "John", last = "Doe") {
    println "Hello, $first $last"
    clo()
}

// usage
foo(clo: { println "I didn't see you there!" }, first: "Jerred", last: "Shepherd")

I’ve seen methods that look a bit prettier when called, like this:

def bar(someArg, Closure clo) {
    clo()
}

bar(someArg) {
    println 'hello'
}

Note that the closure is passed as the last argument. This allows the closure to be passed in braces after the method call. This looks quite a bit nicer!

Inspired by Xe's blog

Originally, I wanted to combine named parameters, default parameters, and a closure as the last argument. Unfortunately, this doesn’t seem possible. Here’s what I came up with:

def baz(first = "John", last = "Doe", Closure clo) {
    println "Hello, $first $last"
    clo()
}

The result of running this:

baz(first: "Jerred", last: "Shepherd") {
  println "I didn't see you there!"
}
Hello, [first:Jerred, last:Shepherd] Doe
I didn't see you there!

Groovy implements named parameters as a map. Unfortunately, it seems that Groovy is using the both the first and last parameters as a map value and passing that to the first argument.

// Groovy takes this:
baz(first: "Jerred", last: "Shepherd")

// and implicitly converts it to this:
baz([first: "Jerred", last: "Shepherd"], null)

// so, when calling baz, we're passing the map as the first argument and null as the second argument
// this leads to Groovy using the map as the first argument, and the default value of "Doe" as the second argument
Hello, [first:Jerred, last:Shepherd] Doe
I didn't see you there!

// instead, we want Groovy to print:
Hello, Jerred Shepherd
I didn't see you there!

Closures are still pretty cool, but it’s frustrating to figure out the syntax for how some of these features interact. Additionally, the feedback loop for Groovy with Jenkins is long, so if you don’t know the language, it’s hard to make progress.

Recent posts from blogs that I like

iTerm2 and AI hype overload

iTerm2 is the most popular terminal emulator for macOS machines. I've used it for years and it has gotten out of my way. It's great software. Recently an update was released that among other things includes new AI integration: AI Add AI-powered natural language command generation. Enter a prompt in the composer and select Edit > Engage Artificial Intelligence. You will need to provide an OpenAI API key since GPT costs money to use. A …

via Xe Iaso's blog May 21, 2024

Copyleft licenses are not “restrictive”

One may observe an axis, or a “spectrum”, along which free and open source software licenses can be organized, where one end is “permissive” and the other end is “copyleft”. It is important to acknowledge, however, that though copyleft can be found at the opposite end of an axis with respect to permissive, it is not synonymous with the linguistic antonym of permissive – that is, copyleft licenses are not “restrictive” by comparison with permissive licenses. Aside: Free software is not synonymous with copyle…

via Drew DeVault's blog April 19, 2024

How web bloat impacts users with slow devices

In 2017, we looked at how web bloat affects users with slow connections. Even in the U.S., many users didn't have broadband speeds, making much of the web difficult to use. It's still the case that many users don't have broadband speeds, both inside and outside of the U.S. and that much of the modern web isn't usable for people with slow internet, but the exponential increase in bandwidth (Nielsen suggests this is 50% per year for high-end connections) has outpaced web bloat for typical sites, making this l…

via danluu.com March 16, 2024