• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Groovy - Basic Question

 
Sri Yasasvi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am new to Groovy. I am running some basic examples while learning.

Could anyone of you tell me why the last print statement is printing true.

def noParam = { -> println "I'm executed"; return "no param"}
def oneParam = { out -> println "Executed with a ${out.class} parameter"; out << "one param"}
println noParam != "no param // Printing true

Thanks,
Yasasvi

 
Sri Yasasvi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my undrestanding.

noParam is Closure. So the statement (println noParam != "no param") is returning true.

Then i changed the statement (println noParam.toString() != "no param") . But it is still returning true. Could you please explain this.

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unlike simple code blocks, closures have to be executed. Try invoking call() on the closure and assign the result to some variable
 
Sri Yasasvi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Mohamed!!!


Now i understand clearly.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic