1

A kind question, can anyone explain the difference between

def login() { 

     //some codes here 

}

to

def login = {

    //some codes here

}

in Grails. Thanks!

Raven
  • 41
  • 1
  • 2
  • 9
  • Sorry, I'm just kind a new in groovy and grails programming. – Raven Jan 06 '17 at 01:16
  • http://stackoverflow.com/questions/9205209/why-should-grails-actions-be-declared-as-methods-instead-of-closures-and-what-di – V H Jan 06 '17 at 10:53
  • You would most likely want to stick with methods instead of closures unless you have some very specific usecase, grails no longer recommends closure actions and may be even removed in future versions. – Sudhir N Jan 09 '17 at 12:35
  • Please, @Mystique, if Joshua Moore gave a satisfactory answer, be a Mensch and confirm. – Luis Muñiz Jan 12 '17 at 22:50
  • sorry @loteq for not acknowledging Joshua Moore's answer right away. thanks – Raven Jan 16 '17 at 01:47

1 Answers1

4

The difference is: def login() { .. } is a method while def login = { ... } is a closure. Older versions of Grails controllers used closures for their actions while later versions use methods (which is where you likely see this difference).

If you want to learn more, read up on Groovy closures.

Joshua Moore
  • 24,706
  • 6
  • 50
  • 73
  • And methods preferred as actions over closures. – Sudhir N Jan 08 '17 at 07:16
  • 1
    It depends on what problem you are trying to solve. Closures are different than methods and thus can be used differently. Unless you have a reason to use a closure you should just use a method. That's my personal advice to you. – Joshua Moore Jan 08 '17 at 08:02
  • Yup, thats what i am saying, in most of situations methods are preffered over closures, and thts why grails team decided to go with methods – Sudhir N Jan 09 '17 at 05:31
  • It's more than just preference as to /why/ controllers were moved to methods instead of closures, but that's not really relevant. Hope the answer addresses your question (if so, you might want to mark it as accepted). – Joshua Moore Jan 09 '17 at 05:38
  • Here's my answer which is copied from grails docs and this says it all why methods were preferred over closures http://stackoverflow.com/a/9205312/1001816 – Sudhir N Jan 09 '17 at 12:32
  • 1
    Thanks @Joshua Moore , it helps a lot. – Raven Jan 16 '17 at 01:46
  • @Mystique, no worries. You're welcome. Glad it helped. – Joshua Moore Jan 16 '17 at 01:56