beansense (1) [Avatar] Offline

I had a very basic question on Listing 2.3 from Sec 2.3.2 - Using scripts.
My question on the snippet below is - assuming the Book class is on the classpath as required, how can Groovy tell that book is of type Book?


Book gina = new Book('Groovy in Action')

assert gina.getTitle() == 'Groovy in Action'
assert getTitleBackwards(gina) == 'noitcA ni yvoorG'

String getTitleBackwards(book) {
//Question -- how does Groovy know book is of Type Book?
title = book.getTitle()
return title.reverse()


Mittie (397) [Avatar] Offline
Re: Qs on using scripts.
Well, chapter 2 is an introduction that doesn't explain all details since we have to start somewhere smilie Your question relates to typing (chapter 3) and method dispatch (chapter 7).

In short, the Groovy compiler doesn't need to know the type.
You as a programmer are free to choose between staying Java-like or being fully dynamic:

String getTitleBackwards(book) { ... // dynamic, using (Object book) effectively

String getTitleBackwards(Book book) {... // Java-like

thanks for posting