The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

225035 (5) [Avatar] Offline
#1
I've come across some Spock code that is of the form:
 with (object1, {
              !member1
              member2 == SomeEnum.ENTRY1.name()
              member3 == SomeEnum.ENTRY2.name()
              isBooleanMember
        })


Here the with clause has an object followed by a comma before the opening brace. What is the difference, if any, of having a comma follow the object being evaluated?
Kostis Kapelonis (63) [Avatar] Offline
#2
Hello

It is exactly the same. This is not a Spock feature, but rather a Groovy feature.
Groovy allows you to omit parentheses and also take the closure out of them if it is the last argument.

The following are all equal:

with(warehouseInventory) {
		!isEmpty()
		getBoxesMovedToday() == 3
		availableOfProduct("toshiba") == 2
		availableOfProduct("panasonic") == 3
		availableOfProduct("jvc") == 2
	}

with warehouseInventory, {
		!isEmpty()
		getBoxesMovedToday() == 3
		availableOfProduct("toshiba") == 2
		availableOfProduct("panasonic") == 3
		availableOfProduct("jvc") == 2
	}

with(warehouseInventory, {
		!isEmpty()
		getBoxesMovedToday() == 3
		availableOfProduct("toshiba") == 2
		availableOfProduct("panasonic") == 3
		availableOfProduct("jvc") == 2
	})



For simplicity I have chosen only one style for the book. Feel free to choose the one you like, but if you work in a team make sure
that everybody agrees on your selection.

For more information see

http://mrhaki.blogspot.gr/2009/11/groovy-goodness-passing-closures-to.html
http://mrhaki.blogspot.gr/2009/10/groovy-goodness-optional-parenthesis.html
http://groovy-lang.org/style-guide.html
225035 (5) [Avatar] Offline
#3
Thank you!