erwaman
erwaman

Reputation: 3557

Unexpected behavior using `with` with CliBuilder

Given a script test.groovy

def cli = new CliBuilder().with {}
println cli

cli = new CliBuilder()
cli.with {}
println cli

when I run groovy test.groovy, the output is

null
groovy.util.CliBuilder@3c22fc4c

Why is the first output line null? Here is my groovy --version info:

Groovy Version: 2.4.3 JVM: 1.8.0_40 Vendor: Oracle Corporation OS: Mac OS X

Upvotes: 0

Views: 63

Answers (2)

Roman
Roman

Reputation: 6656

You are not returning anything from the closure passed to with method, so with returns null, and cli in the first example becomes null.

Upvotes: 0

dmahapatro
dmahapatro

Reputation: 50245

It is null because with(Closure c) should return the delegate (here delegate being an instance of CliBuilder) which will be assigned to cli. Use as:

def cli = new CliBuilder().with { it }

Upvotes: 2

Related Questions